/********************************************************************************
Discrimination in Multi-Phase Systems: Evidence from Child Protection

Created on: 12/28/2022
Last Modified on: 2/13/2024

Description: This program generates all of the tables in our paper, except for 
Table A12, which is generated in Matlab. 

Note that we have removed the file directory names from this program for 
confidentiality reasons.
********************************************************************************/

**************************
**(0) SETUP
**************************
clear
set more off
macro drop all
capture log close
set seed 02042023

*Set directories 
global cleandata 
global tmpdata 
global output 


**************************
**TABLE 1A
**************************
use "${cleandata}screener_analysis_sample_child_call.dta", clear 

*Set up locals for table 
local child white female age
local inv phyab neglect maltreatment edu family medical counselor law court mdhhs provider other_r
local treatment screened fc  
local outcomes inv6m 

**Set up table
putexcel set "${output}QJE_tables.xlsx", sheet("Table 1a") modify 
putexcel B1="All Children"
putexcel C1="Black Children"
putexcel D1="White Children"

**Child characteristics 
local row=2
foreach x of local child {
	local lab: variable label `x'
	putexcel A`row'="`lab'"
	
	sum `x' 
	putexcel B`row'=`r(mean)'
	
	sum `x' if white==0
	putexcel C`row'=`r(mean)'
	
	sum `x' if white==1
	putexcel D`row'=`r(mean)'

	
	local ++row
}

**Investigation characteristics
local ++row
foreach x of local inv {
	local lab: variable label `x'
	putexcel A`row'="`lab'"
	
	sum `x' 
	putexcel B`row'=`r(mean)'
	
	sum `x' if white==0
	putexcel C`row'=`r(mean)'
	
	sum `x' if white==1
	putexcel D`row'=`r(mean)'

	
	local ++row
}



**Treatment rates
local ++row
foreach x of local treatment {
	local lab: variable label `x'
	putexcel A`row'="`lab'"
	
	sum `x' 
	putexcel B`row'=`r(mean)'
	
	sum `x' if white==0
	putexcel C`row'=`r(mean)'
	
	sum `x' if white==1
	putexcel D`row'=`r(mean)'	
	
	local ++row
}


**Outcomes
local ++row
foreach x of local outcomes {
	local lab: variable label `x'
	putexcel A`row'="`lab'"
	
	sum `x' 
	putexcel B`row'=`r(mean)'
	
	sum `x' if white==0
	putexcel C`row'=`r(mean)'
	
	sum `x' if white==1
	putexcel D`row'=`r(mean)'
	
	local ++row
}



**Number and share of observations
local ++row
putexcel A`row'="Observations"
count 
putexcel B`row'=`r(N)'
count if white==0
putexcel C`row'=`r(N)'
count if white==1
putexcel D`row'=`r(N)'



**************************
**TABLE 1B
**************************
use "${cleandata}inv_analysis_sample_child_inv.dta", clear 

*Set up locals for table
local child white female age_inv
local inv phyab neglect maltreat parent rel notrel num_children 
local treatment fc 
local outcomes inv6m

*Set up table
putexcel set "${output}QJE_tables.xlsx", sheet("Table 1b") modify 
putexcel B1="All Children"
putexcel C1="Black Children"
putexcel D1="White Children"

local row=2
foreach x of local child {
	local lab: variable label `x'
	putexcel A`row'="`lab'"
	
	sum `x' 
	putexcel B`row'=`r(mean)'
	
	sum `x' if white==0
	putexcel C`row'=`r(mean)'
	
	sum `x' if white==1
	putexcel D`row'=`r(mean)'
	
	local ++row
}

local ++row
foreach x of local inv {
	local lab: variable label `x'
	putexcel A`row'="`lab'"
	
	sum `x' 
	putexcel B`row'=`r(mean)'
	
	sum `x' if white==0
	putexcel C`row'=`r(mean)'
	
	sum `x' if white==1
	putexcel D`row'=`r(mean)'
	
	local ++row
}

local ++row
foreach x of local treatment {
	local lab: variable label `x'
	putexcel A`row'="`lab'"
	
	sum `x' 
	putexcel B`row'=`r(mean)'
	
	sum `x' if white==0
	putexcel C`row'=`r(mean)'
	
	sum `x' if white==1
	putexcel D`row'=`r(mean)'
	
	local ++row
}

local ++row
foreach x of local outcomes {
	local lab: variable label `x'
	putexcel A`row'="`lab'"
	
	sum `x' 
	putexcel B`row'=`r(mean)'
	
	sum `x' if white==0
	putexcel C`row'=`r(mean)'
	
	sum `x' if white==1
	putexcel D`row'=`r(mean)'
	
	local ++row
}

local ++row
putexcel A`row'="Observations"
count 
putexcel B`row'=`r(N)'
count if white==0
putexcel C`row'=`r(N)'
count if white==1
putexcel D`row'=`r(N)'



**************************
**TABLE 2
**************************
capture file close fh
capture macro drop fh
capture erase "${output}table2.tex"
file open fh using "${output}table2.tex", write replace

*Estimate maltreatment risk by race and UDs in screener decisions, using the screener sample 

*Begin by estimating maltreatment risk by race 
use "${cleandata}screener_placement_maltreatment_rates_se_qje.dta", clear 
count
local n_screeners = r(N)

	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_fe_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin=`r(mean)'
			local `r'_ate_lin=`r(mean)'
	}
	
	foreach type in lin {
			sum w_ate_`type'
			local txt_w_ate_`type'_screen = string(r(mean), "%10.3fc")
			display "txt_w_ate_`type'_screen"
			
			sum b_ate_`type'
			local txt_b_ate_`type'_screen = string(r(mean), "%10.3fc")
			display "txt_b_ate_`type'_screen"
	}
	
*Bootstrap standard errors for maltreatment potential by race 
use "${cleandata}screener_placement_maltreatment_rates_se_qje.dta", clear
	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
	// bootstrapped standard errors
		tempfile temp
		save `temp', replace		
		forvalues b = 1/500 {

			use `temp', replace
			
			*adding noise to screener-specific rates 
			replace D_b = D_b + rnormal(0,b_twse_fc)
			replace D_w = D_w + rnormal(0,w_twse_fc)
			replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
			replace Y_w = Y_w + rnormal(0,w_twse_inv6m)
	
			foreach r in b w {
			
				*inverse variance of the investigator FE
				gen `r'_var=1/(`r'_se_fe_inv6m^2)
				
				*grid to plot fitted values
				sum D_`r'
				range `r'_points `r(min)' 1 100

				*linear regression
				reg Y_`r' D_`r' [aw=`r'_var]
				local ate_`r'_lin=_b[_cons]+_b[D_`r']			
			}
				
			local ate_lin=`ate_b_lin'*`bshare'+`ate_w_lin'*(1-`bshare')
			
			local index = 1
			keep if _n==1
			foreach r in "w" "b" {
				gen ate_`r'_lin=`ate_`r'_lin'
			}
			
			tempfile boot_`b'
			save `boot_`b'', replace	
		}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	
		
		local index =1 			
		foreach type in lin {
			sum ate_w_`type'
			local se_w_ate_`type'_screen = string(r(sd), "%10.3fc")
			
			sum ate_b_`type'
			local se_b_ate_`type'_screen = string(r(sd), "%10.3fc")
			
			
			local ++index
		}


*Now, get estimates of screener UDs
use "${cleandata}screener_screening_maltreatment_rates_qje.dta", clear //162 screeners 
//their *screen-in* rates and subsequent maltreatment rates, conditional on the call being screened out//
rename (b_screened w_screened b_inv6m w_inv6m) (D_b D_w Y_b Y_w)

foreach x in D_b D_w {
	replace `x' = 1-`x'
}

	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
*ATEs are obtained via extrapolation above:
foreach type in lin {	
gen ate_w = `w_ate_`type'' 
gen ate_b = `b_ate_`type'' 
gen ate = ate_w * (1-bshare) + ate_b *(bshare)
gen delta1_w= (D_w*Y_w)/ate_w
gen delta1_b=(D_b*Y_b)/ate_b 
gen delta0_w = (D_w*(1-Y_w))/(1-ate_w) 
gen delta0_b = (D_b*(1-Y_b))/(1-ate_b) 

foreach x in delta1_w delta1_b delta0_w delta0_b {
	replace `x' = 1 - `x'
}

*Gen disparities in screening rates:
gen delta1_screen = delta1_b - delta1_w 
gen delta0_screen = delta0_b - delta0_w 
gen delta_screen = delta1_screen*ate + delta0_screen*(1-ate) 

foreach x in delta1_screen delta0_screen delta_screen {
	sum `x' [aw=count_inv]
	local `type'_`x' = string(`r(mean)', "%10.3fc")		
	display "`type'_`x'"
}
	cap drop ate* delta*
}

*Get standard errors for estimates of screener UDs (the uncertainty comes from extrapolating ATEs in the full sample)
use "${cleandata}screener_screening_maltreatment_rates_qje.dta", clear
rename (b_screened w_screened b_inv6m w_inv6m) (D_b D_w Y_b Y_w)

foreach x in D_b D_w {
	replace `x' = 1-`x'
}
	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	keep bshare D_b D_w Y_b Y_w count_inv 
	
		tempfile temp
		save `temp', replace		
		forvalues b = 1/500 {

		use "${cleandata}screener_placement_maltreatment_rates_se_qje.dta", clear
			*add noise to screener-specific reates 
			replace D_b = D_b + rnormal(0,b_twse_fc)
			replace D_w = D_w + rnormal(0,w_twse_fc)
			replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
			replace Y_w = Y_w + rnormal(0,w_twse_inv6m)
	
			foreach r in w b {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_fe_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			local `r'_ate_lin=`r(mean)'
	}
	
		use `temp', replace
	
		foreach type in lin {
	
		gen ate_w_`type' = `w_ate_`type'' 
		gen ate_b_`type' = `b_ate_`type'' 
		gen ate_`type' = ate_w_`type' * (1-bshare) + ate_b_`type' *(bshare)
		gen delta1_w_`type'= (D_w*Y_w)/ate_w_`type'
		gen delta1_b_`type'=(D_b*Y_b)/ate_b_`type' 
		gen delta0_w_`type' = (D_w*(1-Y_w))/(1-ate_w_`type') 
		gen delta0_b_`type' = (D_b*(1-Y_b))/(1-ate_b_`type') 

	foreach x in delta1_w_`type' delta1_b_`type' delta0_w_`type' delta0_b_`type' {
		replace `x' = 1 - `x'
	}			
			// generate UDs
			gen ud_boot_delta1_`type'= delta1_b_`type' - delta1_w_`type' 
			gen ud_boot_delta0_`type'=delta0_b_`type' - delta0_w_`type' 
			gen ud_boot_delta_`type' =ud_boot_delta1_`type'*`ate_`type'' + ud_boot_delta0_`type'*(1-`ate_`type'') 
			
			// means 
			foreach x in delta0_`type' delta1_`type' delta_`type' {
				sum ud_boot_`x' [aw=count_inv]
				gen avg_ud_boot_`x'_case = `r(mean)' if _n==1
			}

			keep if _n==1
			
			tempfile boot_`b'
			save `boot_`b'', replace	
		}
	}
		
		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}
		
		foreach type in delta_lin {
			sum avg_ud_boot_`type'_case
			local se_`type'_screen = string(r(sd), "%10.3fc")
			local ++index
		}	
		

*Next, estimate screened-in maltreatment risk by race and UD in investigator decisions, using the investigator sample 
	use "${cleandata}inv_adjusted_rates_se_omitted_qje.dta", clear
	
	count 
	local n_inv = r(N)
	
	sum bshare 
	local bshare = r(mean)
	
	rename (w_nofc b_nofc w_inv6m b_inv6m) (D_w D_b Y_w Y_b)
	
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	
	*Estimate ATEs in screened-in sample:
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100
			
			*Linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}

	foreach type in lin {
			sum w_ate_`type'
			local txt_w_ate_`type'_inv = string(r(mean), "%10.3fc")
			
			sum b_ate_`type'
			local txt_b_ate_`type'_inv = string(r(mean), "%10.3fc")			
		}
		
		*Generate delta j's:
		gen ate_lin =  bshare*`b_ate_lin' + (1-bshare)*`w_ate_lin'
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen beta_gap0_lin_w=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen beta_gap0_lin_b=(D_b*(1-Y_b))/(1-`b_ate_lin')
		
		gen delta_lin = (beta_gap0_lin_w * (1 - ate_lin) + beta_gap1_lin_w *(ate_lin))  - ///
		(beta_gap0_lin_b * (1 - ate_lin) + beta_gap1_lin_b * (ate_lin))
		

	// case-weighted mean
	foreach type in lin {
		sum delta_`type' [aw=count_inv]
		local `type'_delta_inv = string(`r(mean)', "%10.3fc")
	}
		
	*Bootstrap standard errors
	use "${cleandata}inv_adjusted_rates_se_omitted_qje.dta", clear
	rename (w_nofc b_nofc w_inv6m b_inv6m) (D_w D_b Y_w Y_b)
	
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	
	// bootstrapped standard errors
		tempfile temp
		save `temp', replace		
		forvalues b = 1/500 {

			use `temp', replace
			
			*add noise to investigator-specific rates 
			replace D_b = D_b + rnormal(0,b_twse_nofc)
			replace D_w = D_w + rnormal(0,w_twse_nofc)
			replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
			replace Y_w = Y_w + rnormal(0,w_twse_inv6m)
	
			foreach r in b w {
			
				*inverse variance of the investigator FE
				gen `r'_var=1/(`r'_se_inv6m^2)
				
				*grid to plot fitted values
				sum D_`r'
				range `r'_points `r(min)' 1 100
				
				*linear regression
				reg Y_`r' D_`r' [aw=`r'_var]
				gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
				sum `r'_fitted_1 if `r'_points==1
				gen `r'_ate_lin = `r(mean)'
				local ate_`r'_lin=`r(mean)'				
				}
				
			local ate_lin=`ate_b_lin'*`bshare'+`ate_w_lin'*(1-`bshare')
						
			// generate UDs
			gen boot1_w = ((D_w*Y_w)/`ate_w_lin')
			gen boot1_b = ((D_b*Y_b)/`ate_b_lin')
			gen boot0_w = (D_w*(1-Y_w))/(1-`ate_w_lin')
			gen boot0_b = (D_b*(1-Y_b))/(1-`ate_b_lin')			
			
			gen ud_boot_lin=(boot1_w - boot1_b)*`ate_lin' ///
			   +(boot0_w - boot0_b)*(1-`ate_lin') 
			
			// case weighted means 
			foreach type in lin {
				sum ud_boot_`type' [aw=count_inv]
				gen avg_ud_boot_`type'_case = `r(mean)' if _n==1
			
			}
			
			keep avg_*
			keep if _n==1
			foreach r in "w" "b" {
				gen ate_`r'_lin=`ate_`r'_lin'
			}
			
			tempfile boot_`b'
			save `boot_`b'', replace	
		}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	
		
		foreach type in lin {
			sum ate_w_`type'
			local se_w_ate_`type'_inv = string(r(sd), "%10.3fc")
			
			sum ate_b_`type'
			local se_b_ate_`type'_inv = string(r(sd), "%10.3fc")
			
			sum avg_ud_boot_`type'_case
			local se_delta_`type'_inv= string(r(sd), "%10.3fc")
			
		}


*Now, compute placement UD and each KOB decomposition, using a combination of these estimates
*(1) Need average maltreatment risk in the full population of calls 
use "${cleandata}screener_placement_maltreatment_rates_se_qje.dta", clear 
count
local n_screeners = r(N)

	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
	*Estimate ATEs among the full set of calls 
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_fe_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			local `r'_ate_lin = `r(mean)'
			gen `r'_ate_lin=`r(mean)'
	}

	local ate_lin = (1-`bshare')*w_ate_lin + `b_ate_lin'*`bshare'
	
	foreach type in lin {
			sum w_ate_`type'
			local w_ate_`type'_screen = string(r(mean), "%10.3fc")
			display "w_ate_`type'_screen"
			
			sum b_ate_`type'
			local b_ate_`type'_screen = string(r(mean), "%10.3fc")
			display "b_ate_`type'_screen"
			
			gen ate_`type' = w_ate_`type' * (1-`bshare') + b_ate_`type' * `bshare'
			sum ate_`type'
			local ate_`type'_screen = string(r(mean), "%10.3fc")
			display "ate_`type'_screen"			
	}

*(2) Need screen-in disparity and conditional screen-in rates 
use "${cleandata}screener_screening_maltreatment_rates_qje.dta", clear 
rename (b_screened w_screened b_inv6m w_inv6m) (D_b D_w Y_b Y_w)
foreach x in D_b D_w {
	replace `x' = 1-`x'
}
	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
*ATEs are obtained via extrapolation:
foreach type in lin {
gen ate_w = `w_ate_`type'' 
gen ate_b = `b_ate_`type'' 
gen ate = `ate_`type''
gen delta1_w_screen= (D_w*Y_w)/ate_w
gen delta1_b_screen=(D_b*Y_b)/ate_b 
gen delta0_w_screen = (D_w*(1-Y_w))/(1-ate_w) 
gen delta0_b_screen = (D_b*(1-Y_b))/(1-ate_b) 

foreach x in delta1_w_screen delta1_b_screen delta0_w_screen delta0_b_screen {
	replace `x' = 1 - `x'
}

*Gen disparities in screening rates:
gen delta1_screen_avg = delta1_b_screen - delta1_w_screen 
gen delta0_screen_avg = delta0_b_screen - delta0_w_screen 
gen delta_screen_avg = delta1_screen_avg*ate + delta0_screen_avg*(1-ate) 
}

	foreach x in delta1_w_screen delta1_b_screen delta0_w_screen delta0_b_screen delta1_screen_avg delta0_screen_avg delta_screen_avg {
		sum `x' [aw=count_inv]
		local `x' = string(r(mean), "%10.3fc")
	}
		
*(3) Need screened-in placement disparities and conditional screened-in placement rates 
	use "${cleandata}inv_adjusted_rates_se_omitted_qje.dta", clear
	count 
	local n_inv = r(N)
	
	sum bshare 
	local bshare = r(mean)
	
	rename (w_nofc b_nofc w_inv6m b_inv6m) (D_w D_b Y_w Y_b)
	
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	
	*Estimate ATEs among screneed-in calls 
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100
			
			*Linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}

		*Generate delta j's:
		gen delta1_w_inv=(D_w*Y_w)/`w_ate_lin'
		gen delta1_b_inv=(D_b*Y_b)/`b_ate_lin' 
		gen delta0_w_inv=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen delta0_b_inv=(D_b*(1-Y_b))/(1-`b_ate_lin')
		
		foreach x in delta1_w_inv delta1_b_inv delta0_w_inv delta0_b_inv {
			replace `x' = 1 - `x'
		}
		
		gen delta1_inv_avg = delta1_b_inv - delta1_w_inv  
		gen delta0_inv_avg = delta0_b_inv - delta0_w_inv
		
	// case-weighted mean
	foreach type in delta1_w_inv delta1_b_inv delta0_w_inv delta0_b_inv delta1_inv_avg delta0_inv_avg  {
		sum `type' [aw=count_inv]
		local `type' = string(`r(mean)', "%10.3fc")
	}
	
*First decomposition
	local first_totUD1 = `delta1_screen_avg'*`delta1_w_inv' + `delta1_inv_avg'*`delta1_b_screen'
	local first_totUD0 = `delta0_screen_avg'*`delta0_w_inv' + `delta0_inv_avg'*`delta0_b_screen'
	local first_sc_sh_UD1 = (`delta1_screen_avg'*`delta1_w_inv')/`first_totUD1'
	local first_sc_sh_UD0 = (`delta0_screen_avg'*`delta0_w_inv')/`first_totUD0'
	display `first_totUD1' `first_totUD0' `first_sc_sh_UD1' `first_sc_sh_UD0'
	local first_totUD = `first_totUD1'*`ate_lin_screen' + `first_totUD0'*(1-`ate_lin_screen')
	local txt_first_totUD = string(`first_totUD1'*`ate_lin_screen' + `first_totUD0'*(1-`ate_lin_screen'),"%10.3fc")
	display `first_totUD'
	
	local first_sc_UD = `delta1_screen_avg'*`delta1_w_inv'*`ate_lin_screen' + (`delta0_screen_avg'*`delta0_w_inv')*(1-`ate_lin_screen')
	local first_sc_sh_UD = `first_sc_UD'/`first_totUD'
	local txt_first_sc_sh_UD = string(`first_sc_UD'/`first_totUD', "%10.3fc")
	local txt_first_inv_sh_UD = string(1 - `first_sc_sh_UD', "%10.3fc")
	
*Second decomposition
	local sec_totUD1 = `delta1_screen_avg'*`delta1_b_inv' + `delta1_inv_avg'*`delta1_w_screen'
	local sec_totUD0 = `delta0_screen_avg'*`delta0_b_inv' + `delta0_inv_avg'*`delta0_w_screen'
	local sec_sc_sh_UD1 = (`delta1_screen_avg'*`delta1_b_inv')/`sec_totUD1'
	local sec_sc_sh_UD0 = (`delta0_screen_avg'*`delta0_b_inv')/`sec_totUD0'
	display `sec_totUD1' `sec_totUD0' `sec_sc_sh_UD1' `sec_sc_sh_UD0'
	local sec_totUD = `sec_totUD1'*`ate_lin_screen' + `sec_totUD0'*(1-`ate_lin_screen')	
	local txt_sec_totUD = string(`sec_totUD1'*`ate_lin_screen' + `sec_totUD0'*(1-`ate_lin_screen'),"%10.3fc")
	display `sec_totUD'
	
	local sec_sc_UD = `delta1_screen_avg'*`delta1_b_inv'*`ate_lin_screen' + (`delta0_screen_avg'*`delta0_b_inv')*(1-`ate_lin_screen')
	local txt_sec_sc_sh_UD = string(`sec_sc_UD'/`sec_totUD', "%10.3fc")
	local sec_sc_sh_UD = `sec_sc_UD'/`sec_totUD'
	display `sec_sc_sh_UD'
	local txt_sec_inv_sh_UD = string(1 - `sec_sc_sh_UD', "%10.3fc")
	

*Bootstrap standard errors for placement UD and decomposition: 
forvalues b = 1/500 {
*(1) Need average maltreatment risk in the full population of calls 
use "${cleandata}screener_placement_maltreatment_rates_se_qje.dta", clear 
count
local n_screeners = r(N)

	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
			*adding noise to screener-specific rates 
			replace D_b = D_b + rnormal(0,b_twse_fc)
			replace D_w = D_w + rnormal(0,w_twse_fc)
			replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
			replace Y_w = Y_w + rnormal(0,w_twse_inv6m)
			
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_fe_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			local `r'_ate_lin = `r(mean)'
			gen `r'_ate_lin=`r(mean)'
	}

	local ate_lin = (1-`bshare')*w_ate_lin + `b_ate_lin'*`bshare'
	
	foreach type in lin {
			sum w_ate_`type'
			local w_ate_`type'_screen = string(r(mean), "%10.3fc")
			display "w_ate_`type'_screen"
			
			sum b_ate_`type'
			local b_ate_`type'_screen = string(r(mean), "%10.3fc")
			display "b_ate_`type'_screen"
			
			gen ate_`type' = w_ate_`type' * (1-`bshare') + b_ate_`type' * `bshare'
			sum ate_`type'
			local ate_`type'_screen = string(r(mean), "%10.3fc")
			display "ate_`type'_screen"			
	}

*(2) Need screen-in disparity and conditional screen-in rates 
use "${cleandata}screener_screening_maltreatment_rates_qje.dta", clear 
rename (b_screened w_screened b_inv6m w_inv6m) (D_b D_w Y_b Y_w)
foreach x in D_b D_w {
	replace `x' = 1-`x'
}

	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
*ATEs are obtained via extrapolation:
foreach type in lin {
gen ate_w = `w_ate_`type'' 
gen ate_b = `b_ate_`type'' 
gen ate = `ate_`type''
gen delta1_w_screen= (D_w*Y_w)/ate_w
gen delta1_b_screen=(D_b*Y_b)/ate_b 
gen delta0_w_screen = (D_w*(1-Y_w))/(1-ate_w) 
gen delta0_b_screen = (D_b*(1-Y_b))/(1-ate_b) 

foreach x in delta1_w_screen delta1_b_screen delta0_w_screen delta0_b_screen {
	replace `x' = 1 - `x'
}

*Gen disparities in screening rates:
gen delta1_screen_avg = delta1_b_screen - delta1_w_screen 
gen delta0_screen_avg = delta0_b_screen - delta0_w_screen 
gen delta_screen_avg = delta1_screen_avg*ate + delta0_screen_avg*(1-ate) 
}

	foreach x in delta1_w_screen delta1_b_screen delta0_w_screen delta0_b_screen delta1_screen_avg delta0_screen_avg delta_screen_avg {
		sum `x' [aw=count_inv]
		local `x' = string(r(mean), "%10.3fc")
	}

	
*(3) Need placement disparities among screened-in children and conditional screened-in placement rates 
	use "${cleandata}inv_adjusted_rates_se_omitted_qje.dta", clear
	
	count 
	local n_inv = r(N)
	
	sum bshare 
	local bshare = r(mean)
	
	rename (w_nofc b_nofc w_inv6m b_inv6m) (D_w D_b Y_w Y_b)
	
	replace D_b = D_b + rnormal(0,b_twse_nofc)
	replace D_w = D_w + rnormal(0,w_twse_nofc)
	replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
	replace Y_w = Y_w + rnormal(0,w_twse_inv6m)
			
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	
	*Estimate ATEs among screened-in children 
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100
			
			*Linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}

		*Generate delta j's:
		gen delta1_w_inv=(D_w*Y_w)/`w_ate_lin'
		gen delta1_b_inv=(D_b*Y_b)/`b_ate_lin' 
		gen delta0_w_inv=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen delta0_b_inv=(D_b*(1-Y_b))/(1-`b_ate_lin')
		
		foreach x in delta1_w_inv delta1_b_inv delta0_w_inv delta0_b_inv {
			replace `x' = 1 - `x'
		}
		
		gen delta1_inv_avg = delta1_b_inv - delta1_w_inv  
		gen delta0_inv_avg = delta0_b_inv - delta0_w_inv
		
	// case-weighted mean
	foreach type in delta1_w_inv delta1_b_inv delta0_w_inv delta0_b_inv delta1_inv_avg delta0_inv_avg  {
		sum `type' [aw=count_inv]
		local `type' = string(`r(mean)', "%10.3fc")
	}
	

*First decomposition
	local first_totUD1 = `delta1_screen_avg'*`delta1_w_inv' + `delta1_inv_avg'*`delta1_b_screen'
	local first_totUD0 = `delta0_screen_avg'*`delta0_w_inv' + `delta0_inv_avg'*`delta0_b_screen'
	local first_sc_sh_UD1 = (`delta1_screen_avg'*`delta1_w_inv')/`first_totUD1'
	local first_sc_sh_UD0 = (`delta0_screen_avg'*`delta0_w_inv')/`first_totUD0'
	display `first_totUD1' `first_totUD0' `first_sc_sh_UD1' `first_sc_sh_UD0'
	local first_totUD = `first_totUD1'*`ate_lin_screen' + `first_totUD0'*(1-`ate_lin_screen')

	local first_sc_UD = `delta1_screen_avg'*`delta1_w_inv'*`ate_lin_screen' + (`delta0_screen_avg'*`delta0_w_inv')*(1-`ate_lin_screen')
	local first_sc_sh_UD = string(`first_sc_UD'/`first_totUD', "%10.3fc")
	display `first_sc_sh_UD'
	local first_inv_sh_UD = string(1 - `first_sc_sh_UD', "%10.3fc")
	
*Second decomposition
	local sec_totUD1 = `delta1_screen_avg'*`delta1_b_inv' + `delta1_inv_avg'*`delta1_w_screen'
	local sec_totUD0 = `delta0_screen_avg'*`delta0_b_inv' + `delta0_inv_avg'*`delta0_w_screen'
	local sec_sc_sh_UD1 = (`delta1_screen_avg'*`delta1_b_inv')/`sec_totUD1'
	local sec_sc_sh_UD0 = (`delta0_screen_avg'*`delta0_b_inv')/`sec_totUD0'
	display `sec_totUD1' `sec_totUD0' `sec_sc_sh_UD1' `sec_sc_sh_UD0'
	local sec_totUD = `sec_totUD1'*`ate_lin_screen' + `sec_totUD0'*(1-`ate_lin_screen')	
	display `sec_totUD'
	
	local sec_sc_UD = `delta1_screen_avg'*`delta1_b_inv'*`ate_lin_screen' + (`delta0_screen_avg'*`delta0_b_inv')*(1-`ate_lin_screen')
	local sec_sc_sh_UD = string(`sec_sc_UD'/`sec_totUD', "%10.3fc")
	display `sec_sc_sh_UD'
	local sec_inv_sh_UD = string(1 - `sec_sc_sh_UD', "%10.3fc")
	
	gen first_totUD = `first_totUD' if _n==1 
	gen sec_totUD = `sec_totUD' if _n==1 
	gen first_sc_sh_UD = `first_sc_sh_UD' if _n==1
	gen first_inv_sh_UD = `first_inv_sh_UD' if _n==1
	gen sec_sc_sh_UD = `sec_sc_sh_UD' if _n==1
	gen sec_inv_sh_UD = `sec_inv_sh_UD' if _n==1
	
	keep first_totUD sec_totUD first_sc_sh_UD first_inv_sh_UD sec_sc_sh_UD sec_inv_sh_UD
	keep if _n==1
	
			tempfile boot_`b'
			save `boot_`b'', replace	
		}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	

		foreach x in first_totUD sec_totUD first_sc_sh_UD first_inv_sh_UD sec_sc_sh_UD sec_inv_sh_UD {
			sum `x'
			local se_`x' = string(r(sd), "%10.3fc")	
			display `se_`x''
		}

		// make table
		file write fh ///
"\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}" _n ///
"\begin{table}[htbp]" _n ///
	"\small"  _n ///
  "\centering" _n ///
  "\caption{Estimates of Mean Maltreatment Risk and Unwarranted Disparity}" _n ///
    "\begin{threeparttable}" _n ///
    "\begin{tabular}{lP{4cm}P{4cm}}" _n ///
	      "\hline \hline " _n ///
          "& \textbf{(1)} & \textbf{(2)} \\" _n ///
		  "\cmidrule{2-3} " _n ///
          "& \multicolumn{2}{c}{\textit{Panel A: Subsequent maltreatment risk}} \\" _n ///
          "& \textbf{All calls} & \textbf{Screened-in calls} \\" _n ///
    "Black children & `txt_b_ate_lin_screen'      & `txt_b_ate_lin_inv' \\" _n ///
					"& (`se_b_ate_lin_screen')    & (`se_b_ate_lin_inv') 		\\" _n ///
   " White children & `txt_w_ate_lin_screen'      & `txt_w_ate_lin_inv' \\" _n ///
					"& (`se_w_ate_lin_screen')    &  (`se_w_ate_lin_inv') 	\\[1em]" _n ///
          "& \multicolumn{2}{c}{\textit{Panel B: Unwarranted disparity UD}} \\" _n ///
									"& \textbf{Screeners} & \textbf{Investigators} \\" _n ///
    "Average across decision-makers &  `lin_delta_screen'  		& `lin_delta_inv' \\" _n ///
								"& (`se_delta_lin_screen')      & (`se_delta_lin_inv') \\[1em]" _n ///
          "& \multicolumn{2}{c}{\textit{Panel C: Placement UD and decompositions}}   \\" _n ///
								"& \textbf{Equation (\ref{eq:first_decomp})} & \textbf{Equation (\ref{eq:second_decomp})} \\" _n ///
    "Placement UD 							& `txt_first_totUD'    		& `txt_sec_totUD'  \\" _n ///
    " 								& (`se_first_totUD')    		& (`se_sec_totUD')  \\" _n ///
    "Screener share 				& `txt_first_sc_sh_UD' 				& `txt_sec_sc_sh_UD' \\" _n ///
    "				 				& (`se_first_sc_sh_UD') 		& (`se_sec_sc_sh_UD') \\" _n ///	
    "Investigator share 			& `txt_first_inv_sh_UD'     		&  `txt_sec_inv_sh_UD' \\" _n ///
    "					 			& (`se_first_inv_sh_UD')     	&  (`se_sec_inv_sh_UD') \\" _n ///
	"&       &  \\" _n ///
    "Number of screeners 			& `n_screeners'   & `n_screeners'  \\" _n ///
    "Number of investigators 		&  `n_inv'     & `n_inv' \\" _n 

	 file write fh ///
	"\hline \hline" _n /// 
	"   \end{tabular}%" _n ///
	"\begin{tablenotes}" _n ///
   " \item \footnotesize" _n ///
	"Notes. This table reports estimates of mean maltreatment risk and unwarranted disparity in both phases of the CPS system. Panel A reports estimates of race-specific average subsequent maltreatment risk, both in the full population of calls and in the population of screened-in calls. These estimates come from the linear extrapolations in Figure \ref{fig:extrapolation}. Panel B reports estimates of average overall screener and investigator UD (weighted by the number of cases assigned to each screener and investigator, respectively). Panel C reports overall placement UD in the full population of cases, as well as the share of placement UD that is due to screeners' decisions according to Equations \eqref{eq:first_decomp} and \eqref{eq:second_decomp}. Robust standard errors for estimates of maltreatment risk in the population of all calls, as well as average screener UD, are two-way clustered at the child and screener level. Robust standard errors for estimates of maltreatment risk in the population of screened-in calls, as well as average investigator UD, are two-way clustered at the child and investigator level. Robust standard errors for estimates of overall placement UD, as well as the percent of placement UD due to screeners and investigators, reflect uncertainty in the underlying estimates of both average screener and investigator UDs. All standard errors are obtained via a bootstrapping procedure (with 500 replications) and appear in parentheses. \\" _n ///
	"\end{tablenotes}" _n ///
	"    \end{threeparttable}" _n ///
	"  \label{tab:main_ud_estimates}%" _n ///
	"\end{table}%" _n _n _n _n _n 

file close fh
macro drop fh


**************************
**TABLE 3 
**************************
capture file close fh
capture macro drop fh
capture erase "${output}table3.tex"
file open fh using "${output}table3.tex", write replace

*Estimate screener UDs and standard errors, by maltreatment potential
use "${cleandata}screener_placement_maltreatment_rates_se_qje.dta", clear 
count
local n_screeners = r(N)

	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_fe_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin=`r(mean)'
			local `r'_ate_lin=`r(mean)'
	}
				
	foreach type in lin {
			sum w_ate_`type'
			local txt_w_ate_`type'_screen = string(r(mean), "%10.3fc")
			display "txt_w_ate_`type'_screen"
			
			sum b_ate_`type'
			local txt_b_ate_`type'_screen = string(r(mean), "%10.3fc")
			display "txt_b_ate_`type'_screen"
	}
	
use "${cleandata}screener_screening_maltreatment_rates_qje.dta", clear 
rename (b_screened w_screened b_inv6m w_inv6m) (D_b D_w Y_b Y_w)
foreach x in D_b D_w {
	replace `x' = 1-`x'
}

	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
*ATEs are obtained via extrapolation:
foreach type in lin {
gen ate_w = `w_ate_`type'' 
gen ate_b = `b_ate_`type'' 
gen ate = ate_w*(1-bshare) + ate_b*(bshare)
gen delta1_w= (D_w*Y_w)/ate_w
gen delta1_b=(D_b*Y_b)/ate_b 
gen delta0_w = (D_w*(1-Y_w))/(1-ate_w) 
gen delta0_b = (D_b*(1-Y_b))/(1-ate_b) 

foreach x in delta1_w delta1_b delta0_w delta0_b {
	replace `x' = 1 - `x'
}

*Gen disparities in screening rates:
gen delta1_screen = delta1_b - delta1_w 
gen delta0_screen = delta0_b - delta0_w 
gen delta_screen = delta1_screen*ate + delta0_screen*(1-ate) 
}
foreach x in delta1_screen delta0_screen delta_screen {
	sum `x' [aw=count_inv]
	local `x'_lin = string(`r(mean)', "%10.3fc")		
	display ``x'_lin'
}

*Get standard errors for estimates of screener UDs (the uncertainty comes from extrapolating ATEs in the full sample)
use "${cleandata}screener_screening_maltreatment_rates_qje.dta", clear
rename (b_screened w_screened b_inv6m w_inv6m) (D_b D_w Y_b Y_w)

foreach x in D_b D_w {
	replace `x' = 1-`x'
}
	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	keep bshare D_b D_w Y_b Y_w count_inv 
	
		tempfile temp
		save `temp', replace		
		forvalues b = 1/500 {

		use "${cleandata}screener_placement_maltreatment_rates_se_qje.dta", clear
			*add noise to screener-specific estimates 
			replace D_b = D_b + rnormal(0,b_twse_fc)
			replace D_w = D_w + rnormal(0,w_twse_fc)
			replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
			replace Y_w = Y_w + rnormal(0,w_twse_inv6m)
	
			foreach r in w b {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_fe_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			local `r'_ate_lin=`r(mean)'
	}
		display `bshare'
		local ate_lin = `w_ate_lin'*(1-`bshare') + `b_ate_lin'*`bshare'
		
		use `temp', replace
	
		foreach type in lin {
	
		gen ate_w_`type' = `w_ate_`type'' 
		gen ate_b_`type' = `b_ate_`type'' 
		gen ate_`type' = ate_w_`type' * (1-bshare) + ate_b_`type' *(bshare)
		gen delta1_w_`type'= (D_w*Y_w)/ate_w_`type'
		gen delta1_b_`type'=(D_b*Y_b)/ate_b_`type' 
		gen delta0_w_`type' = (D_w*(1-Y_w))/(1-ate_w_`type') 
		gen delta0_b_`type' = (D_b*(1-Y_b))/(1-ate_b_`type') 

	foreach x in delta1_w_`type' delta1_b_`type' delta0_w_`type' delta0_b_`type' {
		replace `x' = 1 - `x'
	}			
			// generate UDs
			gen ud_boot_delta1_`type'= delta1_b_`type' - delta1_w_`type' 
			gen ud_boot_delta0_`type'=delta0_b_`type' - delta0_w_`type' 
			gen ud_boot_delta_`type' =ud_boot_delta1_`type'*`ate_`type'' + ud_boot_delta0_`type'*(1-`ate_`type'') 
			
			// means 
			foreach x in delta0_`type' delta1_`type' delta_`type' {
				sum ud_boot_`x' [aw=count_inv]
				gen avg_ud_boot_`x'_case = `r(mean)' if _n==1
			}

			keep if _n==1
			
			tempfile boot_`b'
			save `boot_`b'', replace	
		}
	}
		
		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}
		
		foreach type in lin {
			sum avg_ud_boot_delta_`type'_case
			local se_delta_`type'_screen = string(r(sd), "%10.3fc")
			display "`se_delta_`type'_screen'"
			
			sum avg_ud_boot_delta1_`type'_case
			local se_delta1_`type'_screen = string(r(sd), "%10.3fc")
			display "`se_delta1_`type'_screen'"
			
			sum avg_ud_boot_delta0_`type'_case
			local se_delta0_`type'_screen = string(r(sd), "%10.3fc")
			display "`se_delta0_`type'_screen'"
		}	
		

*Estimate investigator UDs and standard errors, by maltreatment potential
	use "${cleandata}inv_adjusted_rates_se_omitted_qje.dta", clear
	count 
	local n_inv = r(N)
	
	sum bshare 
	local bshare = r(mean)
	
	rename (w_nofc b_nofc w_inv6m b_inv6m) (D_w D_b Y_w Y_b)
	
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	
	*Estimate race-specific ATEs among screened-in children 	
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100
			
			*Linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}
				
		*Generate delta j's:
		gen ate_lin =  bshare*`b_ate_lin' + (1-bshare)*`w_ate_lin'
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen beta_gap0_lin_w=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen beta_gap0_lin_b=(D_b*(1-Y_b))/(1-`b_ate_lin')
		
		gen delta_lin = (beta_gap0_lin_w * (1 - ate_lin) + beta_gap1_lin_w *(ate_lin))  - ///
		(beta_gap0_lin_b * (1 - ate_lin) + beta_gap1_lin_b * (ate_lin))
		
		gen delta1_lin = beta_gap1_lin_w - beta_gap1_lin_b 

		gen delta0_lin = beta_gap0_lin_w - beta_gap0_lin_b 
		
		
	// case-weighted mean
	foreach type in delta_lin delta1_lin delta0_lin {
		sum `type' [aw=count_inv]
		local `type'_inv = string(`r(mean)', "%10.3fc")
	}
	
	*Bootstrap standard errors
	use "${cleandata}inv_adjusted_rates_se_omitted_qje.dta", clear
	rename (w_nofc b_nofc w_inv6m b_inv6m) (D_w D_b Y_w Y_b)
	
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	
	// bootstrapped standard errors
		tempfile temp
		save `temp', replace		
		forvalues b = 1/500 {

			use `temp', replace
			
			*Add noise to investigator-specific estimates 
			replace D_b = D_b + rnormal(0,b_twse_nofc)
			replace D_w = D_w + rnormal(0,w_twse_nofc)
			replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
			replace Y_w = Y_w + rnormal(0,w_twse_inv6m)
	
			foreach r in b w {
			
				*inverse variance of the investigator FE
				gen `r'_var=1/(`r'_se_inv6m^2)
				
				*grid to plot fitted values
				sum D_`r'
				range `r'_points `r(min)' 1 100
				
				*linear regression
				reg Y_`r' D_`r' [aw=`r'_var]
				gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
				sum `r'_fitted_1 if `r'_points==1
				gen `r'_ate_lin = `r(mean)'
				local ate_`r'_lin=`r(mean)'				
				}
				
			local ate_lin=`ate_b_lin'*bshare+`ate_w_lin'*(1-bshare)
						
			// generate UDs
			gen boot1_w = ((D_w*Y_w)/`ate_w_lin')
			gen boot1_b = ((D_b*Y_b)/`ate_b_lin')
			gen boot0_w = (D_w*(1-Y_w))/(1-`ate_w_lin')
			gen boot0_b = (D_b*(1-Y_b))/(1-`ate_b_lin')			
			
			gen ud_boot_lin=(boot1_w - boot1_b)*`ate_lin' ///
			   +(boot0_w - boot0_b)*(1-`ate_lin') 
			
			gen ud_boot1_lin=(boot1_w - boot1_b)
			   
			gen ud_boot0_lin=(boot0_w - boot0_b)
			
			// case weighted means 
			foreach type in ud_boot_lin ud_boot1_lin ud_boot0_lin {
				sum `type' [aw=count_inv]
				gen avg_`type' = `r(mean)' if _n==1
			
			}
			
			keep avg_*
			keep if _n==1
			foreach r in "w" "b" {
				gen ate_`r'_lin=`ate_`r'_lin'
			}
			
			tempfile boot_`b'
			save `boot_`b'', replace	
		}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	
	
		foreach type in ud_boot_lin ud_boot1_lin ud_boot0_lin {
			sum avg_`type'
			local se_`type'= string(r(sd), "%10.3fc")
			
		}

*Estimate placement UDs and standard errors, by maltreatment potential  
*(1) Need average maltreatment risk in the full population of calls 
use "${cleandata}screener_placement_maltreatment_rates_se_qje.dta", clear 
count
local n_screeners = r(N)

	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_fe_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			local `r'_ate_lin = `r(mean)'
			gen `r'_ate_lin=`r(mean)'
	}

	local ate_lin = (1-`bshare')*w_ate_lin + `b_ate_lin'*`bshare'
	
	foreach type in lin {
			sum w_ate_`type'
			local w_ate_`type'_screen = string(r(mean), "%10.3fc")
			display "w_ate_`type'_screen"
			
			sum b_ate_`type'
			local b_ate_`type'_screen = string(r(mean), "%10.3fc")
			display "b_ate_`type'_screen"
			
			gen ate_`type' = w_ate_`type' * (1-`bshare') + b_ate_`type' * `bshare'
			sum ate_`type'
			local ate_`type'_screen = string(r(mean), "%10.3fc")
			display "ate_`type'_screen"			
	}

*(2) Need screen-in disparity and conditional screen-in rates 
use "${cleandata}screener_screening_maltreatment_rates_qje.dta", clear 
rename (b_screened w_screened b_inv6m w_inv6m) (D_b D_w Y_b Y_w)
foreach x in D_b D_w {
	replace `x' = 1-`x'
}
	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
*ATEs are obtained via extrapolation:
foreach type in lin {
gen ate_w = `w_ate_`type'' 
gen ate_b = `b_ate_`type'' 
gen ate = `ate_`type''
gen delta1_w_screen= (D_w*Y_w)/ate_w
gen delta1_b_screen=(D_b*Y_b)/ate_b 
gen delta0_w_screen = (D_w*(1-Y_w))/(1-ate_w) 
gen delta0_b_screen = (D_b*(1-Y_b))/(1-ate_b) 

foreach x in delta1_w_screen delta1_b_screen delta0_w_screen delta0_b_screen {
	replace `x' = 1 - `x'
}

*Gen disparities in screening rates:
gen delta1_screen_avg = delta1_b_screen - delta1_w_screen 
gen delta0_screen_avg = delta0_b_screen - delta0_w_screen 
gen delta_screen_avg = delta1_screen_avg*ate + delta0_screen_avg*(1-ate) 
}

	foreach x in delta1_w_screen delta1_b_screen delta0_w_screen delta0_b_screen delta1_screen_avg delta0_screen_avg delta_screen_avg {
		sum `x' [aw=count_inv]
		local `x' = string(r(mean), "%10.3fc")
	}

*(3) Need placement disparities and conditional placement rates among screened-in children 
	use "${cleandata}inv_adjusted_rates_se_omitted_qje.dta", clear	
	count 
	local n_inv = r(N)
	
	sum bshare 
	local bshare = r(mean)
	
	rename (w_nofc b_nofc w_inv6m b_inv6m) (D_w D_b Y_w Y_b)
	rename (w_se_nofc b_se_nofc) (w_se_fe_nofc b_se_fe_nofc)
	
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	
	*Estimate race-specific ATEs among screened-in children  
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100
			
			*Linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}

		*Generate delta j's:
		gen delta1_w_inv=(D_w*Y_w)/`w_ate_lin'
		gen delta1_b_inv=(D_b*Y_b)/`b_ate_lin' 
		gen delta0_w_inv=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen delta0_b_inv=(D_b*(1-Y_b))/(1-`b_ate_lin')
		
		foreach x in delta1_w_inv delta1_b_inv delta0_w_inv delta0_b_inv {
			replace `x' = 1 - `x'
		}
		
		gen delta1_inv_avg = delta1_b_inv - delta1_w_inv  
		gen delta0_inv_avg = delta0_b_inv - delta0_w_inv
		
	// case-weighted mean
	foreach type in delta1_w_inv delta1_b_inv delta0_w_inv delta0_b_inv delta1_inv_avg delta0_inv_avg  {
		sum `type' [aw=count_inv]
		local `type' = string(`r(mean)', "%10.3fc")
	}
	
*First decomposition (either first or second decomp yield identical estimates of placement UD, by construction)
	local first_totUD1 = `delta1_screen_avg'*`delta1_w_inv' + `delta1_inv_avg'*`delta1_b_screen'
	local first_totUD0 = `delta0_screen_avg'*`delta0_w_inv' + `delta0_inv_avg'*`delta0_b_screen'
	local first_totUD = `first_totUD1'*`ate_lin_screen' + `first_totUD0'*(1-`ate_lin_screen')

	local txt_first_totUD1 = string(`delta1_screen_avg'*`delta1_w_inv' + `delta1_inv_avg'*`delta1_b_screen',"%10.3fc")
	local txt_first_totUD0 = string(`delta0_screen_avg'*`delta0_w_inv' + `delta0_inv_avg'*`delta0_b_screen',"%10.3fc")
	local txt_first_totUD = string(`first_totUD1'*`ate_lin_screen' + `first_totUD0'*(1-`ate_lin_screen'),"%10.3fc")
		

*Bootstrap standard errors for placement UD and decomposition 
forvalues b = 1/500 {
*(1) Need average maltreatment risk in the full population of calls 
use "${cleandata}screener_placement_maltreatment_rates_se_qje.dta", clear 
count
local n_screeners = r(N)
	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
			*add noise to screener-specific rates 
			replace D_b = D_b + rnormal(0,b_twse_fc)
			replace D_w = D_w + rnormal(0,w_twse_fc)
			replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
			replace Y_w = Y_w + rnormal(0,w_twse_inv6m)
			
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_fe_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			local `r'_ate_lin = `r(mean)'
			gen `r'_ate_lin=`r(mean)'
	}

	local ate_lin = (1-`bshare')*w_ate_lin + `b_ate_lin'*`bshare'
	
	foreach type in lin {
			sum w_ate_`type'
			local w_ate_`type'_screen = string(r(mean), "%10.3fc")
			display "w_ate_`type'_screen"
			
			sum b_ate_`type'
			local b_ate_`type'_screen = string(r(mean), "%10.3fc")
			display "b_ate_`type'_screen"
			
			gen ate_`type' = w_ate_`type' * (1-`bshare') + b_ate_`type' * `bshare'
			sum ate_`type'
			local ate_`type'_screen = string(r(mean), "%10.3fc")
			display "ate_`type'_screen"			
	}

*(2) Need screen-in disparity and conditional screen-in rates 
use "${cleandata}screener_screening_maltreatment_rates_qje.dta", clear 
rename (b_screened w_screened b_inv6m w_inv6m) (D_b D_w Y_b Y_w)
foreach x in D_b D_w {
	replace `x' = 1-`x'
}
	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
*ATEs are obtained via extrapolation:
foreach type in lin {
gen ate_w = `w_ate_`type'' 
gen ate_b = `b_ate_`type'' 
gen ate = `ate_`type''
gen delta1_w_screen= (D_w*Y_w)/ate_w
gen delta1_b_screen=(D_b*Y_b)/ate_b 
gen delta0_w_screen = (D_w*(1-Y_w))/(1-ate_w) 
gen delta0_b_screen = (D_b*(1-Y_b))/(1-ate_b) 

foreach x in delta1_w_screen delta1_b_screen delta0_w_screen delta0_b_screen {
	replace `x' = 1 - `x'
}

*Gen disparities in screening rates:
gen delta1_screen_avg = delta1_b_screen - delta1_w_screen 
gen delta0_screen_avg = delta0_b_screen - delta0_w_screen 
gen delta_screen_avg = delta1_screen_avg*ate + delta0_screen_avg*(1-ate) 
}

	foreach x in delta1_w_screen delta1_b_screen delta0_w_screen delta0_b_screen delta1_screen_avg delta0_screen_avg delta_screen_avg {
		sum `x' [aw=count_inv]
		local `x' = string(r(mean), "%10.3fc")
	}

*(3) Need placement disparities and conditional placement rates among screened-in calls 
	use "${cleandata}inv_adjusted_rates_se_omitted_qje.dta", clear
	count 
	local n_inv = r(N)
	
	sum bshare 
	local bshare = r(mean)
	
	rename (w_nofc b_nofc w_inv6m b_inv6m) (D_w D_b Y_w Y_b)
	
	replace D_b = D_b + rnormal(0,b_twse_nofc)
	replace D_w = D_w + rnormal(0,w_twse_nofc)
	replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
	replace Y_w = Y_w + rnormal(0,w_twse_inv6m)
			
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	
	*Estimate race-specific ATEs among screened-in children 
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100
			
			*Linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}

		*Generate delta j's:
		gen delta1_w_inv=(D_w*Y_w)/`w_ate_lin'
		gen delta1_b_inv=(D_b*Y_b)/`b_ate_lin' 
		gen delta0_w_inv=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen delta0_b_inv=(D_b*(1-Y_b))/(1-`b_ate_lin')
		
		foreach x in delta1_w_inv delta1_b_inv delta0_w_inv delta0_b_inv {
			replace `x' = 1 - `x'
		}
		
		gen delta1_inv_avg = delta1_b_inv - delta1_w_inv  
		gen delta0_inv_avg = delta0_b_inv - delta0_w_inv
		
	// case-weighted mean
	foreach type in delta1_w_inv delta1_b_inv delta0_w_inv delta0_b_inv delta1_inv_avg delta0_inv_avg  {
		sum `type' [aw=count_inv]
		local `type' = string(`r(mean)', "%10.3fc")
	}
	
*First decomposition
	gen first_totUD1 = `delta1_screen_avg'*`delta1_w_inv' + `delta1_inv_avg'*`delta1_b_screen' if _n==1
	gen first_totUD0 = `delta0_screen_avg'*`delta0_w_inv' + `delta0_inv_avg'*`delta0_b_screen' if _n==1
	gen first_totUD = `first_totUD1'*`ate_lin_screen' + `first_totUD0'*(1-`ate_lin_screen') if _n==1
	
	keep first_totUD*
	
	keep if _n==1
	
			tempfile boot_`b'
			save `boot_`b'', replace	
		}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	

		foreach x in first_totUD first_totUD1 first_totUD0 {
			sum `x'
			local se_`x' = string(r(sd), "%10.3fc")	
			display `se_`x''
		}

		// make table
		file write fh ///
	"\begin{table}[htbp]" _n ///
	"\small"  _n ///
  "\centering" _n ///
  "\caption{Estimates of UD by Subsequent Maltreatment Potential}" _n ///
      "\begin{threeparttable}" _n ///
   " \begin{tabular}{lccc}" _n ///
		      "\hline \hline " _n ///
          "& \textbf{(1)} & \textbf{(2)} & \textbf{(3)} \\" _n ///
          "& \textbf{Total} & \textbf{With } & \textbf{Without } \\" _n ///
          "&       & \textbf{maltreatment} & \textbf{maltreatment} \\" _n ///
          "&       & \textbf{potential} & \textbf{potential} \\" _n ///
          "\cmidrule{2-4}  \\" _n ///
    "Screener UD 	 & `delta_screen_lin'      &  `delta1_screen_lin'     &  `delta0_screen_lin' \\" _n ///
					"&  (`se_delta_lin_screen')     &  (`se_delta1_lin_screen')     & (`se_delta0_lin_screen') \\" _n ///
					"&       &       &  \\" _n ///
    "Investigator UD &  `delta_lin_inv'     &  `delta1_lin_inv'     & `delta0_lin_inv' \\" _n ///
					"& (`se_ud_boot_lin')      &  (`se_ud_boot1_lin')     & (`se_ud_boot0_lin') \\" _n ///
					"&       &       &  \\" _n ///
    "Placement UD  		 &  `txt_first_totUD'     &  `txt_first_totUD1'     & `txt_first_totUD0' \\" _n ///
					"&  (`se_first_totUD')     & (`se_first_totUD1')      & (`se_first_totUD0') \\" _n ///
					"&       &       &  \\" _n ///
    "Number of screeners & `n_screeners'      & `n_screeners'      & `n_screeners' \\" _n ///
    "Number of investigators &   `n_inv'    &  `n_inv'   & `n_inv'  \\" _n 
	
	 file write fh ///
	"\hline \hline" _n /// 
	"   \end{tabular}%" _n ///
	"\begin{tablenotes}" _n ///
   " \item \footnotesize" _n ///
	"Notes. Notes. This table presents estimates of average screener, investigator, and placement UD separately by maltreatment potential. Specifically, the first row presents estimates of $\Delta^S$, $\Delta_1^S$, and $\Delta_0^S$. The second row presents estimates of $\Delta^D$, $\Delta_1^D$, and $\Delta_0^D$. The third row combines these estimates using Equations \eqref{eq:first_decomp} and \eqref{eq:second_decomp} to report estimates of $\Delta^P$, $\Delta_1^P$, and $\Delta_0^P$. Robust standard errors for estimates of screener UDs are two-way clustered at the child and screener level. Robust standard errors for estimates of investigator UDs are two-way clustered at the child and investigator level. Robust standard errors for estimates of placement UD reflect uncertainty in the underlying estimates of both screener and investigator UDs. All standard errors are obtained via a bootstrapping procedure (with 500 replications) and appear in parentheses. \\" _n ///
	"\end{tablenotes}" _n ///
	"    \end{threeparttable}" _n ///
	"  \label{tab:main_by_potential}%" _n ///
	"\end{table}%" _n _n _n _n _n 

file close fh
macro drop fh


**************************
**TABLE 4 
**************************
capture file close fh
capture macro drop fh
capture erase "${output}table4.tex"
file open fh using "${output}table4.tex", write replace

//Generate estimates of \Delta^D_{j1} and then estimate regression relating \Delta^D_{j1} to investigator covariates//
//We do this in the sample of investigations for which we have investigator demographics  
	use "${cleandata}limited_inv_analysis_sample.dta", clear
	count 
	local n_inv = r(N)			
	rename (w_fe_fc b_fe_fc w_fe_inv6m b_fe_inv6m) (D_w D_b Y_w Y_b)
	
	*Generate additional variables needed for analysis (e.g., "lenient")
	gen D = D_w*(1-sh_black) + D_b*(sh_black)
	sum D, d 
	gen lenient = D >= `r(p50)'

	sum sh_black, d 
	gen highsh = sh_black >= `r(p50)'
	
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	
	sum bshare 
	local bshare = `r(mean)'
	
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_fe_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			local `r'_ate_lin=`r(mean)'
	}
	
		*Generate \Delta^D_{j1}'s:
		*Linear extrapolation:
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen delta1_lin = beta_gap1_lin_w - beta_gap1_lin_b 

*Estimate regressions
reg delta1_lin blackinv otherinv [aw=count_inv], robust
local blackinv = string(_b[blackinv], "%10.3fc")
local otherinv = string(_b[otherinv], "%10.3fc")
sum delta1_lin [aw=count_inv] if whiteinv==1 
local blackinv_cm = string(r(mean), "%10.3fc")

reg delta1_lin femaleinv [aw=count_inv], robust
local femaleinv = string(_b[femaleinv], "%10.3fc")
sum delta1_lin [aw=count_inv] if femaleinv==0 
local femaleinv_cm = string(r(mean), "%10.3fc")

reg delta1_lin newemp [aw=count_inv], robust
local newemp = string(_b[newemp], "%10.3fc")
sum delta1_lin [aw=count_inv] if newemp==0 
local newemp_cm = string(r(mean), "%10.3fc")

reg delta1_lin urban [aw=count_inv], robust
local urban = string(_b[urban], "%10.3fc")
sum delta1_lin [aw=count_inv] if urban==0 
local urban_cm = string(r(mean), "%10.3fc")

reg delta1_lin highsh [aw=count_inv], robust
local highsh = string(_b[highsh], "%10.3fc")
sum delta1_lin [aw=count_inv] if highsh==0 
local highsh_cm = string(r(mean), "%10.3fc")

reg delta1_lin lenient [aw=count_inv], robust
local lenient = string(_b[lenient], "%10.3fc")
sum delta1_lin [aw=count_inv] if lenient==0 
local lenient_cm = string(r(mean), "%10.3fc")

reg delta1_lin blackinv otherinv femaleinv newemp urban highsh lenient [aw=count_inv], robust
local c_blackinv = string(_b[blackinv], "%10.3fc")
local c_otherinv = string(_b[otherinv], "%10.3fc")
local c_femaleinv = string(_b[femaleinv], "%10.3fc")
local c_newemp = string(_b[newemp], "%10.3fc")
local c_urban = string(_b[urban], "%10.3fc")
local c_highsh = string(_b[highsh], "%10.3fc")
local c_lenient = string(_b[lenient], "%10.3fc")

sum delta1_lin [aw=count_inv]
local cm = string(r(mean), "%10.3fc")

*******************************************
*Bootstrap standard errors 
	use "${cleandata}limited_inv_analysis_sample.dta", clear 
	count 
	local n_inv = r(N)
	
	rename (w_fe_fc b_fe_fc w_fe_inv6m b_fe_inv6m) (D_w D_b Y_w Y_b)

	*Generate additional variables needed for analysis (e.g., "lenient")
	gen D = D_w*(1-sh_black) + D_b*(sh_black)
	sum D, d 
	gen lenient = D >= `r(p50)'

	sum sh_black, d 
	gen highsh = sh_black >= `r(p50)'

*save this as a tempfile
		keep D_w D_b Y_w Y_b w_se_fe_inv6m b_se_fe_inv6m w_se_tw_fe_fc b_se_tw_fe_fc w_se_tw_fe_inv6m b_se_tw_fe_inv6m ///
		highsh lenient blackinv otherinv femaleinv urban newemp bshare count_inv whiteinv 
		tempfile temp
		save `temp', replace		
		forvalues b = 1/500 {

			use `temp', replace
			
			*add noise to investigator-specific estimates
			replace D_b = D_b + rnormal(0,b_se_tw_fe_fc)
			replace D_w = D_w + rnormal(0,w_se_tw_fe_fc)
			replace Y_b = Y_b + rnormal(0,b_se_tw_fe_inv6m)
			replace Y_w = Y_w + rnormal(0,w_se_tw_fe_inv6m)
			
		gen D_w2 = D_w*D_w 
		gen D_b2 = D_b*D_b
		
		sum bshare 
		local bshare = `r(mean)'
	
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_fe_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			local `r'_ate_lin=`r(mean)'
	}
	
		*Generate \Delta^D_{j1}'s:
		*Linear extrapolation:
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen delta1_lin = beta_gap1_lin_w - beta_gap1_lin_b 

*Estimate regressions and generate table 
reg delta1_lin blackinv otherinv [aw=count_inv], robust
gen b_blackinv = _b[blackinv] if _n==1
gen b_otherinv = _b[otherinv] if _n==1
sum delta1_lin [aw=count_inv] if whiteinv==1 
gen blackinv_cm = r(mean) if _n==1

reg delta1_lin femaleinv [aw=count_inv], robust
gen b_femaleinv = _b[femaleinv] if _n==1
sum delta1_lin [aw=count_inv] if femaleinv==0 
gen femaleinv_cm = r(mean) if _n==1

reg delta1_lin newemp [aw=count_inv], robust
gen b_newemp = _b[newemp] if _n==1
sum delta1_lin [aw=count_inv] if newemp==0 
gen newemp_cm = r(mean) if _n==1

reg delta1_lin urban [aw=count_inv], robust
gen b_urban = _b[urban] if _n==1
sum delta1_lin [aw=count_inv] if urban==0 
gen urban_cm = r(mean) if _n==1

reg delta1_lin highsh [aw=count_inv], robust
gen b_highsh = _b[highsh] if _n==1
sum delta1_lin [aw=count_inv] if highsh==0 
gen highsh_cm = r(mean) if _n==1

reg delta1_lin lenient [aw=count_inv], robust
gen b_lenient = _b[lenient] if _n==1
sum delta1_lin [aw=count_inv] if lenient==0 
gen lenient_cm = r(mean) if _n==1

reg delta1_lin blackinv otherinv femaleinv newemp urban highsh lenient [aw=count_inv], robust
gen c_blackinv = _b[blackinv] if _n==1
gen c_otherinv = _b[otherinv] if _n==1
gen c_femaleinv = _b[femaleinv] if _n==1
gen c_newemp = _b[newemp] if _n==1
gen c_urban = _b[urban] if _n==1
gen c_highsh = _b[highsh] if _n==1
gen c_lenient = _b[lenient] if _n==1

sum delta1_lin [aw=count_inv] 
gen cm = r(mean) if _n==1

keep b_blackinv b_otherinv b_femaleinv b_newemp b_urban b_highsh b_lenient ///
c_blackinv c_otherinv c_femaleinv c_newemp c_urban c_highsh c_lenient ///
blackinv_cm femaleinv_cm newemp_cm urban_cm highsh_cm lenient_cm cm 

keep if _n==1 
		
			tempfile boot_`b'
			save `boot_`b'', replace	
		}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	
			
		foreach x in b_blackinv b_otherinv b_femaleinv b_newemp b_urban b_highsh b_lenient ///
		c_blackinv c_otherinv c_femaleinv c_newemp c_urban c_highsh c_lenient ///
		blackinv_cm femaleinv_cm newemp_cm urban_cm highsh_cm lenient_cm cm  {
			
			sum `x'
			local se_`x' = string(r(sd), "%10.3fc")
			display `se_`x''
		}
				

		file write fh ///
	"\begin{table}[htbp]" _n ///
  "\centering" _n ///
  "\footnotesize" _n ///
  "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
  "\caption{Regressions of Investigator UD On Investigator Characteristics}" _n ///
    "\begin{threeparttable}" _n ///
    "\begin{tabular}{lccccccc}" _n ///
    "\hline \hline " _n ///
              "& \textbf{(1)} & \textbf{(2)} & \textbf{(3)} & \textbf{(4)} & \textbf{(5)} & \textbf{(6)} & \textbf{(7)} \\" _n ///
          "\cmidrule{2-8} " _n ///
    "Black investigator 	& `blackinv' &       &       &       &       &       			& `c_blackinv' \\" _n ///
	"					& (`se_b_blackinv') 		&       &       &       &       &       			& (`se_c_blackinv') \\" _n ///
    "Other race 			& `otherinv' &       &       &       &       &       			& `c_otherinv' \\" _n ///
						"& (`se_b_otherinv') 		&       &       &       &       &       			& (`se_c_otherinv') \\" _n ///
    "Female investigator &       & `femaleinv' &       &       &       &       			& `c_femaleinv' \\" _n ///
	"					&       & (`se_b_femaleinv') &       &       &       &       					& (`se_c_femaleinv') \\" _n ///
    "New investigator 	&       &       & `newemp' &       &       &       				& `c_newemp' \\" _n ///
						"&       &       & (`se_b_newemp') &       &       &       					& (`se_c_newemp') \\" _n ///
    "Urban county 		&       &       &       & `urban' &       &       				& `c_urban' \\" _n ///
						"&       &       &       & (`se_b_urban') &       &       					& (`se_c_urban') \\" _n ///
    "High share of Black children &       &       &       &       & `highsh' &       	& `c_highsh' \\" _n ///
								 "&       &       &       &       & (`se_b_highsh') &       		& (`se_c_highsh') \\" _n ///
    "Low placement rate 			 &       &       &       &       &       & `lenient' 	& `c_lenient' \\" _n ///
								 "&       &       &       &       &       & (`se_b_lenient') 		& (`se_c_lenient') \\" _n ///
    "Control mean  & `blackinv_cm' & `femaleinv_cm' & `newemp_cm' & `urban_cm' & `highsh_cm' & `lenient_cm' & `cm' \\" _n ///
    "Number of investigators  & `n_inv'   & `n_inv'   & `n_inv'   & `n_inv'   & `n_inv'   & `n_inv'   & `n_inv' \\" _n 
	
	 file write fh ///
    "\hline \hline " _n ///
    "\end{tabular}%" _n ///
"\begin{tablenotes}" _n ///
 "\item \footnotesize Notes. This table reports OLS estimates of regressions of estimates of $\Delta^{D}_{j1}$ (based on a linear extrapolation) on investigator characteristics. As discussed in Section \ref{sect: data}, a subset of MDHHS data (from 2008 to 2017) contains the names of investigators, which we use to predict demographic characteristics. This table focuses on the 699 investigators for whom we have such information. Except for Column 7, the ``control mean'' refers to the average value of dependent variable, weighted by the number of investigations assigned to each investigator, for the omitted category. The omitted category in Column 1 is an indicator variable for whether the investigator is white. 86\% of investigators in our sample are white, 12\% are Black, and 2\% are another race, such as Hispanic or Asian. Column 7 reports the (weighted) average value of the dependent variable across all investigators in this sample. New investigators are defined as those hired during our estimation period. There are 457 new investigators in our sample. Investigators with a high share of Black children are those whose caseloads have an above-median share of Black children. Low placement rate investigators are those with a placement rate below the sample median, controlling for rotation fixed effects. All specifications are weighted by the number of investigations assigned to the specific investigator. Robust standard errors, two-way clustered by child and investigator, are reported in parentheses and are obtained via a bootstrapping procedure (with 500 replications)." _n ///
"\end{tablenotes}" _n ///
"\end{threeparttable} " _n ///
"  \label{tab:het_inv}%  " _n ///
"\end{table}%" _n _n _n _n _n 

file close fh
macro drop fh


**************************
**TABLE A1
**************************
use "${cleandata}screener_analysis_sample_child_call.dta", clear 
global covs female i.age phyab neglect maltreatment edu family medical counselor law court mdhhs provider other_r cw_time_stata miss_* 

*For Black children:
reghdfe screened z $covs if white==0, absorb(rotation) cluster(childpartyid screener)
test z
local fstat=round(r(F),0.001)
outreg2 using "${tmpdata}screener_balance.dta", dta label nocons dec(3) keep($covs) ///
	addtext(F Stat from Joint Test, N, P-Value from Joint Test, N, White, N, Black, Y, First Stage Fstat, `fstat')

reghdfe z $covs if white==0, absorb(rotation) cluster(childpartyid screener) 
testparm $covs  
local p=round(r(p),.001)
local f=round(r(F),.001)
outreg2 using "${tmpdata}screener_balance.dta", dta label nocons dec(3) keep($covs) ///
	addtext(F Stat from Joint Test, `f', P-Value from Joint Test, `p', White, N, Black, Y, First Stage Fstat, N)
	
*For white children:
reghdfe screened z $covs if white==1, absorb(rotation) cluster(childpartyid screener)
test z
local fstat=round(r(F),0.001)
outreg2 using "${tmpdata}screener_balance.dta", dta label nocons dec(3) keep($covs) ///
	addtext(F Stat from Joint Test, N, P-Value from Joint Test, N, White, Y, Black, N, First Stage Fstat, `fstat')

reghdfe z $covs if white==1, absorb(rotation) cluster(childpartyid screener) 
testparm $covs 
local p=round(r(p),.001)
local f=round(r(F),.001)
outreg2 using "${tmpdata}screener_balance.dta", dta label nocons dec(3) keep($covs) ///
	addtext(F Stat from Joint Test, `f', P-Value from Joint Test, `p', White, Y, Black, N, First Stage Fstat, N)

preserve
use "${tmpdata}screener_balance_dta.dta", clear
export excel using "${output}QJE_tables.xlsx", sheet("Table A1", modify)
cap erase "${tmpdata}screener_balance_dta.dta"
cap erase "${tmpdata}screener_balance.dta"
restore


**************************
**TABLE A2
**************************
use "${cleandata}inv_analysis_sample_child_inv.dta", clear
global covs female i.age_inv phyab neglect maltreat parent rel2 notrel num_children miss_* 

*For Black children:
reghdfe fc z $covs if white==0, absorb(rotationgroup) cluster(vicid worker_id)
test z
local fstat=round(r(F))
outreg2 using "${tmpdata}inv_balance.dta", dta label nocons dec(3) keep($covs) ///
	addtext(F Stat from Joint Test, N, P-Value from Joint Test, N, White, N, Black, Y, First Stage Fstat, `fstat')

reghdfe z $covs if white==0, absorb(rotationgroup) cluster(vicid worker_id) 
testparm $covs
local p=round(r(p),.001)
local f=round(r(F),.001)
outreg2 using "${tmpdata}inv_balance.dta", dta label nocons dec(3) keep($covs) ///
	addtext(F Stat from Joint Test, `f', P-Value from Joint Test, `p', White, N, Black, Y, First Stage Fstat, N)
	
*For white children:
reghdfe fc z $covs if white==1, absorb(rotationgroup) cluster(vicid worker_id)
test z
local fstat=round(r(F))
outreg2 using "${tmpdata}inv_balance.dta", dta label nocons dec(3) keep($covs) ///
	addtext(F Stat from Joint Test, N, P-Value from Joint Test, N, White, Y, Black, N, First Stage Fstat, `fstat')

reghdfe z $covs if white==1, absorb(rotationgroup) cluster(vicid worker_id) 
testparm $covs
local p=round(r(p),.001)
local f=round(r(F),.001)
outreg2 using "${tmpdata}inv_balance.dta", dta label nocons dec(3) keep($covs) ///
	addtext(F Stat from Joint Test, `f', P-Value from Joint Test, `p', White, Y, Black, N, First Stage Fstat, N)

preserve
use "${tmpdata}inv_balance_dta.dta", clear
export excel using "${output}QJE_tables.xlsx", sheet("Table A2", replace)
cap erase "${tmpdata}inv_balance_dta.dta"
cap erase "${tmpdata}inv_balance.dta"
restore

**************************
**TABLE A3
**************************
use "${cleandata}child_investigation_analysis_sample.dta", clear

*Merge-in investigator race and gender information
cap drop _merge 
merge m:1 worker_id using "${tmpdata}inv_covariates.dta", keep(1 3) keepus(whiteinv blackinv femaleinv)
cap drop _merge 
gen black = white==0

reghdfe whiteinv white, absorb(rotationgroup) cluster(vicid worker_id)
outreg2 using "${tmpdata}balance_concordance.dta", dta label dec(4) ///

reghdfe blackinv black, absorb(rotationgroup) cluster(vicid worker_id)
outreg2 using "${tmpdata}balance_concordance.dta", dta label dec(4) ///

reghdfe femaleinv female, absorb(rotationgroup) cluster(vicid worker_id)
outreg2 using "${tmpdata}balance_concordance.dta", dta label dec(4) ///

preserve
use "${tmpdata}balance_concordance_dta.dta", clear
export excel using "${output}QJE_tables.xlsx", sheet("Table A3", modify)
cap erase "${tmpdata}balance_concordance_dta.dta"
cap erase "${tmpdata}balance_concordance.dta"
restore


**************************
**TABLE A4 
**************************
*First two columns:
use "${cleandata}screener_analysis_sample_child_call.dta", clear 
global covs female i.age phyab neglect maltreatment edu family medical counselor law court mdhhs provider other_r cw_time_stata miss_* 

*Bivariate regression
reghdfe screened black, cluster(childpartyid screener) noabs
sum screened
local avgsc = r(mean)
outreg2 using "${tmpdata}benchmarking.dta", dta label nocons dec(3) keep(black) ///
	addtext(Rotation Group FE, N, Other Controls, N, Avg Y, `avgsc')

*Add other controls
reghdfe screened black $covs, absorb(rotation) cluster(childpartyid screener)
outreg2 using "${tmpdata}benchmarking.dta", dta label nocons dec(3) keep(black) ///
	addtext(Rotation Group FE, Y, Other Controls, Y, Avg Y, `avgsc')

preserve
use "${tmpdata}benchmarking_dta.dta", clear
export excel using "${output}QJE_tables.xlsx", sheet("Table A4a", replace)
cap erase "${tmpdata}benchmarking.dta"
cap erase "${tmpdata}benchmarking_dta.dta"
restore


*Third and fourth columns: 
use "${cleandata}inv_analysis_sample_child_inv.dta", clear
global covs female i.age_inv phyab neglect maltreat parent rel2 notrel num_children miss_* 
gen black = white==0

*Bivariate regression
reghdfe fc black, cluster(vicid worker_id) noabs
sum fc 
local avgfc = r(mean)
outreg2 using "${tmpdata}benchmarking.dta", dta label nocons dec(3) keep(black) ///
	addtext(Rotation Group FE, N, Other Controls, N, Avg Y, `avgfc')

**Adding controls and rotation fixed effects
reghdfe fc black $covs, absorb(rotationgroup) cluster(vicid worker_id)
outreg2 using "${tmpdata}benchmarking.dta", dta label nocons dec(3) keep(black) ///
	addtext(Rotation Group FE, Y, Other Controls, Y, Avg Y, `avgfc')

preserve
use "${tmpdata}benchmarking_dta.dta", clear
export excel using "${output}QJE_tables.xlsx", sheet("Table A4b", replace)
cap erase "${tmpdata}benchmarking.dta"
cap erase "${tmpdata}benchmarking_dta.dta"
restore


**************************
**TABLE A5
**************************
capture file close fh
capture macro drop fh
capture erase "${output}tableA5.tex"
file open fh using "${output}tableA5.tex", write replace

	local index = 1
	foreach thresh in 99 98 97 {
		
	*First, estimate \mu's in the full sample of calls 
	use "${cleandata}screener_placement_maltreatment_rates_se_qje", clear
	count 
	local n = r(N)
	
	rename (w_se_fe_fc b_se_fe_fc) (w_se_fe_nofc b_se_fe_nofc)
	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
	foreach r in b w {
	
		*inverse variance of the investigator FE
		gen `r'_var=1/(`r'_se_fe_inv6m^2)

		*grid to plot fitted values
		range `r'_points 0.01 1 100

		*local linear regression
		lpoly Y_`r' D_`r' [aw=`r'_var], bw(0.1) k(gaus) degree(1) nograph at(`r'_points) gen(y`r' x`r')
		disp r(bwidth)	
		sum x`r' if y`r'>(`thresh'/100-0.01) & y`r'<(`thresh'/100+0.01)
		local `r'_max_ate_llin`thresh'=string(`=r(mean)*`thresh'/100+1*(1-`thresh'/100)', "%10.3fc")
		local `r'_min_ate_llin`thresh'=string(`=r(mean)*`thresh'/100', "%10.3fc")
		drop `r'_var `r'_points	y`r' x`r'
	}

	local min_ud_ate_b=.
	local min_ud_ate_w=.
	local max_ud_ate_b=.
	local max_ud_ate_w=.	
	local max_ud=-999
	local min_ud=999
	local min_udeb_ate_b=.
	local min_udeb_ate_w=.
	local max_udeb_ate_b=.
	local max_udeb_ate_w=.	
	local max_udeb=-999
	local min_udeb=999
	local min_udsd_ate_b=.
	local min_udsd_ate_w=.
	local max_udsd_ate_b=.
	local max_udsd_ate_w=.	
	local max_udsd=-999
	local min_udsd=999
	local min_udfr_ate_b=.
	local min_udfr_ate_w=.
	local max_udfr_ate_b=.
	local max_udfr_ate_w=.	
	local max_udfr=-999
	local min_udfr=999
	
	forval ate_b=`b_min_ate_llin`thresh''(0.001)`b_max_ate_llin`thresh'' {
		forval ate_w=`w_min_ate_llin`thresh''(0.001)`w_max_ate_llin`thresh'' {
			local ate=`ate_b'*bshare+`ate_w'*(1-bshare)
			
*Now, generate UDs in screener disparities, using these \mu's 
use "${cleandata}screener_screening_maltreatment_rates_qje", clear 
rename (b_screened w_screened b_inv6m w_inv6m) (D_b D_w Y_b Y_w)

	foreach x in D_w D_b {
	replace `x' = 1-`x'
	}
	
	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
				
			qui {
			gen hyp_adj_gap=((D_w*Y_w)/`ate_w' ///
				-(D_b*Y_b)/`ate_b')*`ate' ///
			   +((D_w*(1-Y_w))/(1-`ate_w') ///
				-(D_b*(1-Y_b))/(1-`ate_b'))*(1-`ate')
				
			qui summ hyp_adj_gap [aw=count_inv]
			if r(mean) < `min_ud' {
				local min_ud=r(mean)
				local min_ud_ate_b=`ate_b'
				local min_ud_ate_w=`ate_w'
			}
			if r(mean) > `max_ud' {
				local max_ud=r(mean)
				local max_ud_ate_b=`ate_b'
				local max_ud_ate_w=`ate_w'
			}
						
			foreach x in hyp_adj_gap {
				cap drop `x'
			}
		}
		}
	}
			
	foreach stat in ud udeb udsd udfr {
		local min_ate_llin`thresh'=`min_`stat'_ate_b'*bshare+`min_`stat'_ate_w'*(1-bshare)
		local max_ate_llin`thresh'=`max_`stat'_ate_b'*bshare+`max_`stat'_ate_w'*(1-bshare)
		
		gen ud_min_llin`thresh'=((D_w*Y_w)/`min_`stat'_ate_w' ///
			-(D_b*Y_b)/`min_`stat'_ate_b')*`min_ate_llin`thresh'' ///
		   +((D_w*(1-Y_w))/(1-`min_`stat'_ate_w') ///
			-(D_b*(1-Y_b))/(1-`min_`stat'_ate_b'))*(1-`min_ate_llin`thresh'') 

		gen ud_max_llin`thresh'=((D_w*Y_w)/`max_`stat'_ate_w' ///
			-(D_b*Y_b)/`max_`stat'_ate_b')*`max_ate_llin`thresh'' ///
		   +((D_w*(1-Y_w))/(1-`max_`stat'_ate_w') ///
			-(D_b*(1-Y_b))/(1-`max_`stat'_ate_b'))*(1-`max_ate_llin`thresh'') 
	
		// case-weighted mean
		foreach type in min_llin`thresh' max_llin`thresh' {	
			sum ud_`type' [aw=count_inv]
			if "`stat'"=="ud" {
				local `type'_mn_text_case = string(`r(mean)', "%10.3fc")	
				display "``type'_mn_text_case'"
			}
		}
		
		gen beta_gap_min_llin`thresh'=ud_min_llin`thresh'
		gen beta_gap_max_llin`thresh'=ud_max_llin`thresh'

		summ beta_gap_min_llin`thresh' [aw=count_inv]
		summ beta_gap_max_llin`thresh' [aw=count_inv]

		drop ud_*_llin`thresh' beta_gap_*_llin`thresh' 
	}
	}
	
**Bootstrap standard errors 
	foreach thresh in 99 98 97 {
	forvalues b = 1/500 {
		
	*Estimate \mu's in the full sample of calls 
	use "${cleandata}screener_placement_maltreatment_rates_se_qje", clear
	count 
	local n = r(N)
	
	replace D_b = D_b + rnormal(0,b_twse_fc)
	replace D_w = D_w + rnormal(0,w_twse_fc)
	replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
	replace Y_w = Y_w + rnormal(0,w_twse_inv6m)	
	
	rename (w_se_fe_fc b_se_fe_fc) (w_se_fe_nofc b_se_fe_nofc)
	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
	foreach r in b w {
	
		*inverse variance of the investigator FE
		gen `r'_var=1/(`r'_se_fe_inv6m^2)

		*grid to plot fitted values
		range `r'_points 0.01 1 100

		*local linear regression
		lpoly Y_`r' D_`r' [aw=`r'_var], bw(0.1) k(gaus) degree(1) nograph at(`r'_points) gen(y`r' x`r')
		disp r(bwidth)	
		sum x`r' if y`r'>(`thresh'/100-0.01) & y`r'<(`thresh'/100+0.01)
		local `r'_max_ate_llin`thresh'=string(`=r(mean)*`thresh'/100+1*(1-`thresh'/100)', "%10.3fc")
		local `r'_min_ate_llin`thresh'=string(`=r(mean)*`thresh'/100', "%10.3fc")
		gen `r'_max_ate_llin`thresh'_var=r(mean)*`thresh'/100+1*(1-`thresh'/100)
		gen `r'_min_ate_llin`thresh'_var=r(mean)*`thresh'/100	
		drop `r'_var `r'_points	y`r' x`r'
	}

	local min_ud_ate_b=.
	local min_ud_ate_w=.
	local max_ud_ate_b=.
	local max_ud_ate_w=.	
	local max_ud=-`thresh'9
	local min_ud=`thresh'9
	local min_udeb_ate_b=.
	local min_udeb_ate_w=.
	local max_udeb_ate_b=.
	local max_udeb_ate_w=.	
	local max_udeb=-`thresh'9
	local min_udeb=`thresh'9
	local min_udsd_ate_b=.
	local min_udsd_ate_w=.
	local max_udsd_ate_b=.
	local max_udsd_ate_w=.	
	local max_udsd=-`thresh'9
	local min_udsd=`thresh'9
	local min_udfr_ate_b=.
	local min_udfr_ate_w=.
	local max_udfr_ate_b=.
	local max_udfr_ate_w=.	
	local max_udfr=-`thresh'9
	local min_udfr=`thresh'9
	
keep w_min_ate_llin`thresh'_var w_max_ate_llin`thresh'_var b_min_ate_llin`thresh'_var b_max_ate_llin`thresh'_var
gen boot =`b' 
keep if _n==1 

tempfile tmp 
save `tmp'
	
	forval ate_b=`b_min_ate_llin`thresh''(0.001)`b_max_ate_llin`thresh'' {
		forval ate_w=`w_min_ate_llin`thresh''(0.001)`w_max_ate_llin`thresh'' {
			local ate=`ate_b'*`bshare'+`ate_w'*(1-`bshare')
		
*Now, generate UDs in screener disparities, using these \mu's 
use "${cleandata}screener_screening_maltreatment_rates_qje", clear 
rename (b_screened w_screened b_inv6m w_inv6m) (D_b D_w Y_b Y_w)

	foreach x in D_w D_b {
	replace `x' = 1-`x'
	}
	
	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
			
			qui {
			gen hyp_adj_gap=((D_w*Y_w)/`ate_w' ///
				-(D_b*Y_b)/`ate_b')*`ate' ///
			   +((D_w*(1-Y_w))/(1-`ate_w') ///
				-(D_b*(1-Y_b))/(1-`ate_b'))*(1-`ate')
				
			qui summ hyp_adj_gap [aw=count_inv]
			if r(mean) < `min_ud' {
				local min_ud=r(mean)
				local min_ud_ate_b=`ate_b'
				local min_ud_ate_w=`ate_w'
			}
			if r(mean) > `max_ud' {
				local max_ud=r(mean)
				local max_ud_ate_b=`ate_b'
				local max_ud_ate_w=`ate_w'
			}
						
			foreach x in hyp_adj_gap {
				cap drop `x'
			}
		}
		}
	}
			
	foreach stat in ud udeb udsd udfr {
		local min_ate_llin`thresh'=`min_`stat'_ate_b'*bshare+`min_`stat'_ate_w'*(1-bshare)
		local max_ate_llin`thresh'=`max_`stat'_ate_b'*bshare+`max_`stat'_ate_w'*(1-bshare)
		
		gen ud_min_llin`thresh'=((D_w*Y_w)/`min_`stat'_ate_w' ///
			-(D_b*Y_b)/`min_`stat'_ate_b')*`min_ate_llin`thresh'' ///
		   +((D_w*(1-Y_w))/(1-`min_`stat'_ate_w') ///
			-(D_b*(1-Y_b))/(1-`min_`stat'_ate_b'))*(1-`min_ate_llin`thresh'') 

		gen ud_max_llin`thresh'=((D_w*Y_w)/`max_`stat'_ate_w' ///
			-(D_b*Y_b)/`max_`stat'_ate_b')*`max_ate_llin`thresh'' ///
		   +((D_w*(1-Y_w))/(1-`max_`stat'_ate_w') ///
			-(D_b*(1-Y_b))/(1-`max_`stat'_ate_b'))*(1-`max_ate_llin`thresh'') 
	
		// case-weighted mean
		foreach type in min_llin`thresh' max_llin`thresh' {	
			sum ud_`type' [aw=count_inv]
			if "`stat'"=="ud" {
				gen `type'_var =`r(mean)'
			}
		}
		drop ud_*_llin`thresh' 
	}

	keep min_llin`thresh'_var max_llin`thresh'_var 
	gen boot=`b'
	keep if _n==1 
	merge 1:1 boot using `tmp'
	
			tempfile boot_`b'
			save `boot_`b'', replace
		}


		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	
	
	foreach x in min_llin`thresh' max_llin`thresh' b_max_ate_llin`thresh' b_min_ate_llin`thresh' w_max_ate_llin`thresh' w_min_ate_llin`thresh' {
		sum `x'_var 
		local se_`x' = string(r(sd), "%10.3fc")
		display `se_`x''
	}
}

		file write fh ///
	"\begin{table}[htbp]" _n ///
  "\centering" _n ///
  "\footnotesize" _n ///
  "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
  "\caption{Bounds on Maltreatment Risk and Screener UDs}" _n ///
  "\begin{threeparttable}" _n ///
    "\begin{tabular}{lccc}" _n ///
    "\hline \hline" _n /// 
          "& \textbf{(1)} & \textbf{(2)} & \textbf{(3)} \\" _n ///
          "& \textbf{From 0.03} & \textbf{From 0.02} & \textbf{From 0.01} \\" _n ///
          "& \textbf{Placement Rate} & \textbf{Placement Rate} & \textbf{Placement Rate} \\" _n ///
        "\cmidrule{2-4}" _n ///
          "& \multicolumn{3}{c}{\textit{Panel A: Subsequent maltreatment risk}} \\" _n ///
 "Black children & [`b_min_ate_llin97',`b_max_ate_llin97'] & [`b_min_ate_llin98',`b_max_ate_llin98'] & [`b_min_ate_llin99',`b_max_ate_llin99'] \\" _n ///
				"& (`se_b_min_ate_llin97',`se_b_max_ate_llin97') & (`se_b_min_ate_llin98',`se_b_max_ate_llin98') & (`se_b_min_ate_llin99',`se_b_max_ate_llin99') \\" _n ///
 "White children & [`w_min_ate_llin97',`w_max_ate_llin97'] &  [`w_min_ate_llin98',`w_max_ate_llin98'] & [`w_min_ate_llin99',`w_max_ate_llin99']  \\" _n ///
				"& (`se_w_min_ate_llin97',`se_w_max_ate_llin97') & (`se_w_min_ate_llin98',`se_w_max_ate_llin98') & (`se_w_min_ate_llin99',`se_w_max_ate_llin99')  \\" _n ///
          "&       &       &  \\" _n ///
          "& \multicolumn{3}{c}{\textit{Panel B: Screener UD}} \\" _n ///
 "Mean across screeners & [`min_llin97_mn_text_case',`max_llin97_mn_text_case'] & [`min_llin98_mn_text_case',`max_llin98_mn_text_case']  &  [`min_llin99_mn_text_case',`max_llin99_mn_text_case']    \\" _n ///
							"& (`se_min_llin97',`se_max_llin97') & (`se_min_llin98',`se_max_llin98') & (`se_min_llin99',`se_max_llin99')  \\ \\" _n ///
"Number of screeners  & `n'  & `n'   & `n'   \\" _n 
   
   file write fh ///
        "\hline \hline " _n ///
    "\end{tabular}%" _n ///
  "\begin{tablenotes}" _n ///
 "   \item \footnotesize \textit{Notes.} Panel A reports bounds on race-specific average re-investigation risk in the full population of calls, while Panel B reports bounds on average (case-weighted) screener UD. To estimate bounds on mean risk, Column 1 uses a local linear fit of re-investigation rates among screeners with a 3\% placement rate of Black and white children. Columns 2 and 3 form bounds from screeners with placement rates of 2\% and 1\%, respectively. Bounds are formed under the assumption that either none or all of the children placed in foster care in each column have potential for re-investigation. Panel B searches within these bounds to find the combination of Black and white mean risk that minimize or maximize each screener UD statistic. Robust standard errors on the endpoints of each set of bounds, two-way clustered at the child and screener levels, are obtained by a bootstrapping procedure (with 500 replications) and appear in parentheses. \\" _n ///
"\end{tablenotes}"_n ///
    "\end{threeparttable}" _n ///
  "\label{tab:lenient_screener_bounds}%" _n ///
"\end{table}%" _n _n _n _n _n 

file close fh
macro drop fh


**************************
**TABLE A6
**************************
capture file close fh
capture macro drop fh
capture erase "${output}tableA6.tex"
file open fh using "${output}tableA6.tex", write replace
	
	use "${cleandata}inv_adjusted_rates_se_omitted_qje.dta", clear
	count 
	local n = r(N)
	
	rename (w_nofc b_nofc w_inv6m b_inv6m) (D_w D_b Y_w Y_b)
	rename (w_se_nofc b_se_nofc) (w_se_fe_nofc b_se_fe_nofc)
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	
	sum bshare 
	local bshare = r(mean)
	
	local index = 1
	
	foreach thresh in 99 98 97 { 
	
	foreach r in b w {
	
		*inverse variance of the investigator FE
		gen `r'_var=1/(`r'_se_inv6m^2)

		*grid to plot fitted values
		range `r'_points 0.01 1 100

		*local linear regression
		lpoly Y_`r' D_`r' [aw=`r'_var], bw(0.1) k(gaus) degree(1) nograph at(`r'_points) gen(y`r' x`r')
		disp r(bwidth)	
		sum x`r' if y`r'>(`thresh'/100-0.01) & y`r'<(`thresh'/100+0.01)
		local `r'_max_ate_llin`thresh'=string(`=r(mean)*`thresh'/100+1*(1-`thresh'/100)', "%10.3fc")
		local `r'_min_ate_llin`thresh'=string(`=r(mean)*`thresh'/100', "%10.3fc")
		drop `r'_var `r'_points	y`r' x`r'
	}

	local min_ud_ate_b=.
	local min_ud_ate_w=.
	local max_ud_ate_b=.
	local max_ud_ate_w=.	
	local max_ud=-999
	local min_ud=999
	local min_udeb_ate_b=.
	local min_udeb_ate_w=.
	local max_udeb_ate_b=.
	local max_udeb_ate_w=.	
	local max_udeb=-999
	local min_udeb=999
	local min_udsd_ate_b=.
	local min_udsd_ate_w=.
	local max_udsd_ate_b=.
	local max_udsd_ate_w=.	
	local max_udsd=-999
	local min_udsd=999
	local min_udfr_ate_b=.
	local min_udfr_ate_w=.
	local max_udfr_ate_b=.
	local max_udfr_ate_w=.	
	local max_udfr=-999
	local min_udfr=999
	
	forval ate_b=`b_min_ate_llin`thresh''(0.001)`b_max_ate_llin`thresh'' {
		forval ate_w=`w_min_ate_llin`thresh''(0.001)`w_max_ate_llin`thresh'' {
			local ate=`ate_b'*bshare+`ate_w'*(1-bshare)
			qui {
			gen hyp_adj_gap=((D_w*Y_w)/`ate_w' ///
				-(D_b*Y_b)/`ate_b')*`ate' ///
			   +((D_w*(1-Y_w))/(1-`ate_w') ///
				-(D_b*(1-Y_b))/(1-`ate_b'))*(1-`ate')
				
			qui summ hyp_adj_gap [aw=count_inv]
			if r(mean) < `min_ud' {
				local min_ud=r(mean)
				local min_ud_ate_b=`ate_b'
				local min_ud_ate_w=`ate_w'
			}
			if r(mean) > `max_ud' {
				local max_ud=r(mean)
				local max_ud_ate_b=`ate_b'
				local max_ud_ate_w=`ate_w'
			}
						
			foreach x in hyp_adj_gap {
				cap drop `x'
			}
		}
		}
	}
			
	foreach stat in ud udeb udsd udfr {
		local min_ate_llin`thresh'=`min_`stat'_ate_b'*bshare+`min_`stat'_ate_w'*(1-bshare)
		local max_ate_llin`thresh'=`max_`stat'_ate_b'*bshare+`max_`stat'_ate_w'*(1-bshare)
		
		gen ud_min_llin`thresh'=((D_w*Y_w)/`min_`stat'_ate_w' ///
			-(D_b*Y_b)/`min_`stat'_ate_b')*`min_ate_llin`thresh'' ///
		   +((D_w*(1-Y_w))/(1-`min_`stat'_ate_w') ///
			-(D_b*(1-Y_b))/(1-`min_`stat'_ate_b'))*(1-`min_ate_llin`thresh'') 

		gen ud_max_llin`thresh'=((D_w*Y_w)/`max_`stat'_ate_w' ///
			-(D_b*Y_b)/`max_`stat'_ate_b')*`max_ate_llin`thresh'' ///
		   +((D_w*(1-Y_w))/(1-`max_`stat'_ate_w') ///
			-(D_b*(1-Y_b))/(1-`max_`stat'_ate_b'))*(1-`max_ate_llin`thresh'') 
	
		// case-weighted mean
		foreach type in min_llin`thresh' max_llin`thresh' {	
			sum ud_`type' [aw=count_inv]
			if "`stat'"=="ud" {
				local `type'_mn_text_case = string(`r(mean)', "%10.3fc")	
				display "``type'_mn_text_case'"
			}
		}
		
		gen beta_gap_min_llin`thresh'=ud_min_llin`thresh'
		gen beta_gap_max_llin`thresh'=ud_max_llin`thresh'

		summ beta_gap_min_llin`thresh' [aw=count_inv]
		summ beta_gap_max_llin`thresh' [aw=count_inv]

		drop ud_*_llin`thresh' beta_gap_*_llin`thresh' 
	}
	}
		
// bootstrap standard errors
		keep D_b D_w Y_b Y_w w_se_inv6m b_se_inv6m b_twse_inv6m b_twse_nofc w_twse_inv6m w_twse_nofc bshare count_inv 
		tempfile temp
		save `temp', replace		
		
		forvalues b = 1/500 {

			use `temp', replace
			
			*add noise to investigator-specific rates 
			replace D_b = D_b + rnormal(0,b_twse_nofc)
			replace D_w = D_w + rnormal(0,w_twse_nofc)
			replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
			replace Y_w = Y_w + rnormal(0,w_twse_inv6m)

			foreach thresh in 99 98 97 {
			
			foreach r in b w {
			
				*inverse variance of the investigator FE
				gen `r'_var=1/(`r'_se_inv6m^2)
				
				*grid to plot fitted values
				range `r'_points 0.01 1 100

				*local linear regression
				lpoly Y_`r' D_`r' [aw=`r'_var], bw(0.1) k(gaus) degree(1) nograph at(`r'_points) gen(y`r' x`r')
				disp r(bwidth)	
				
				sum x`r' if y`r'>(`thresh'/100-0.01) & y`r'<(`thresh'/100+0.01)
				local ate_`r'_max_llin`thresh'=`r(mean)'*`thresh'/100+1*(1-`thresh'/100)
				local ate_`r'_min_llin`thresh'=`r(mean)'*`thresh'/100
				drop `r'_var `r'_points y`r' x`r'
			}
				
			local min_ud_ate_b=.
			local min_ud_ate_w=.
			local max_ud_ate_b=.
			local max_ud_ate_w=.	
			local max_ud=-999
			local min_ud=999
			local min_udeb_ate_b=.
			local min_udeb_ate_w=.
			local max_udeb_ate_b=.
			local max_udeb_ate_w=.	
			local max_udeb=-999
			local min_udeb=999
			local min_udsd_ate_b=.
			local min_udsd_ate_w=.
			local max_udsd_ate_b=.
			local max_udsd_ate_w=.	
			local max_udsd=-999
			local min_udsd=999
			local min_udfr_ate_b=.
			local min_udfr_ate_w=.
			local max_udfr_ate_b=.
			local max_udfr_ate_w=.	
			local max_udfr=-999
			local min_udfr=999
	
			forval ate_b=`ate_b_min_llin`thresh''(0.001)`ate_b_max_llin`thresh'' {
				forval ate_w=`ate_w_min_llin`thresh''(0.001)`ate_w_max_llin`thresh'' {
					local ate=`ate_b'*bshare+`ate_w'*(1-bshare)
					gen hyp_adj_gap=((D_w*Y_w)/`ate_w' ///
						-(D_b*Y_b)/`ate_b')*`ate' ///
					   +((D_w*(1-Y_w))/(1-`ate_w') ///
						-(D_b*(1-Y_b))/(1-`ate_b'))*(1-`ate')
						
					qui summ hyp_adj_gap [aw=count_inv]
					if r(mean) < `min_ud' {
						local min_ud=r(mean)
						local min_ud_ate_b=`ate_b'
						local min_ud_ate_w=`ate_w'
					}
					if r(mean) > `max_ud' {
						local max_ud=r(mean)
						local max_ud_ate_b=`ate_b'
						local max_ud_ate_w=`ate_w'
					}
					
					drop hyp_adj_gap				
						
				}
			}
			
			foreach stat in ud udeb udsd udfr {
			
				local min_ate_llin`thresh'=`min_`stat'_ate_b'*bshare+`min_`stat'_ate_w'*(1-bshare)
				local max_ate_llin`thresh'=`max_`stat'_ate_b'*bshare+`max_`stat'_ate_w'*(1-bshare)
		
				// generate UDs
			cap drop ud_boot_min_llin`thresh'
			cap drop ud_boot_max_llin`thresh'
				gen ud_boot_min_llin`thresh'=((D_w*Y_w)/`min_`stat'_ate_w' ///
					-(D_b*Y_b)/`min_`stat'_ate_b')*`min_ate_llin`thresh'' ///
				   +((D_w*(1-Y_w))/(1-`min_`stat'_ate_w') ///
					-(D_b*(1-Y_b))/(1-`min_`stat'_ate_b'))*(1-`min_ate_llin`thresh'') 
				gen ud_boot_max_llin`thresh'=((D_w*Y_w)/`max_`stat'_ate_w' ///
					-(D_b*Y_b)/`max_`stat'_ate_b')*`max_ate_llin`thresh'' ///
				   +((D_w*(1-Y_w))/(1-`max_`stat'_ate_w') ///
					-(D_b*(1-Y_b))/(1-`max_`stat'_ate_b'))*(1-`max_ate_llin`thresh'') 				

				// case weighted means 
				foreach type in min_llin`thresh' max_llin`thresh' {	
					if "`stat'"=="ud" {
					sum ud_boot_`type' [aw=count_inv]
					gen avg_ud_boot_`type'_case = `r(mean)' if _n==1
					}
				}
				}
				drop ud_boot_*_llin`thresh' 
			}
			
			
			keep avg_* 
			keep if _n==1
			foreach thresh in 99 98 97 {
				foreach r in "w" "b" {
					gen ate_`r'_max_llin`thresh'=`ate_`r'_max_llin`thresh''
					gen ate_`r'_min_llin`thresh'=`ate_`r'_min_llin`thresh''
				}
			}

			tempfile boot_`b'
			save `boot_`b'', replace	
			display "`boot_b'"
		}
	

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	

		local index =1 	
		foreach thresh in 99 98 97 {
		foreach type in min_llin`thresh' max_llin`thresh' {
			sum ate_w_`type'
			local se_w_ate_`type' = string(r(sd), "%10.3fc")
			sum ate_b_`type'
			local se_b_ate_`type' = string(r(sd), "%10.3fc")
			sum avg_ud_boot_`type'_case
			local se_mean_`index'_case = string(r(sd), "%10.3fc")
			local ++index
		}
		}
		
		file write fh ///
	"\begin{table}[htbp]" _n ///
  "\centering" _n ///
  "\footnotesize" _n ///
  "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
  "\caption{Bounds on Screened-in Maltreatment Risk and Investigator UDs}" _n ///
  "\begin{threeparttable}" _n ///
    "\begin{tabular}{lccc}" _n ///
    "\hline \hline" _n /// 
          "& \textbf{(1)} & \textbf{(2)} & \textbf{(3)} \\" _n ///
          "& \textbf{From 0.03} & \textbf{From 0.02} & \textbf{From 0.01} \\" _n ///
          "& \textbf{Placement Rate} & \textbf{Placement Rate} & \textbf{Placement Rate} \\" _n ///
        "\cmidrule{2-4}" _n ///
          "& \multicolumn{3}{c}{\textit{Panel A: Subsequent maltreatment risk}} \\" _n ///
 "Black children & [`b_min_ate_llin97',`b_max_ate_llin97'] & [`b_min_ate_llin98',`b_max_ate_llin98'] & [`b_min_ate_llin99',`b_max_ate_llin99'] \\" _n ///
				"& (`se_b_ate_min_llin97',`se_b_ate_max_llin97') & (`se_b_ate_min_llin98',`se_b_ate_max_llin98') & (`se_b_ate_min_llin99',`se_b_ate_max_llin99') \\" _n ///
 "White children & [`w_min_ate_llin97',`w_max_ate_llin97'] &  [`w_min_ate_llin98',`w_max_ate_llin98'] & [`w_min_ate_llin99',`w_max_ate_llin99']  \\" _n ///
				"& (`se_w_ate_min_llin97',`se_w_ate_max_llin97') & (`se_w_ate_min_llin98',`se_w_ate_max_llin98') & (`se_w_ate_min_llin99',`se_w_ate_max_llin99')  \\" _n ///
          "&       &       &  \\" _n ///
          "& \multicolumn{3}{c}{\textit{Panel B: Investigator UD}} \\" _n ///
 "Mean across investigators & [`min_llin97_mn_text_case',`max_llin97_mn_text_case'] & [`min_llin98_mn_text_case',`max_llin98_mn_text_case']  &  [`min_llin99_mn_text_case',`max_llin99_mn_text_case']    \\" _n ///
							"& (`se_mean_1_case',`se_mean_2_case') & (`se_mean_3_case',`se_mean_4_case') & (`se_mean_5_case',`se_mean_6_case')  \\ \\" _n ///
"Number of investigators  & `n'  & `n'   & `n'   \\" _n 
   
   file write fh ///
        "\hline \hline " _n ///
    "\end{tabular}%" _n ///
  "\begin{tablenotes}" _n ///
 "   \item \footnotesize \textit{Notes.} Panel A reports bounds on race-specific average re-investigation risk in the population of screened-in calls, while Panel B reports bounds on average (case-weighted) investigator UD. To estimate bounds on mean risk, Column 1 uses a local linear fit of re-investigation rates among investigators who remove 3\% of screened-in Black and white children. Columns 2 and 3 form bounds from investigators who remove 2\% and 1\% of screened-in Black and white children, respectively. Bounds are formed under the assumption that either none or all of the children placed in foster care in each column have potential for re-investigation. Panel B searches within these bounds to find the combination of Black and white mean risk that minimize or maximize each investigator UD statistic. Robust standard errors on the endpoints of each set of bounds, two-way clustered at the child and investigator levels, are obtained by a bootstrapping procedure (with 500 replications) and appear in parentheses. \\" _n ///
"\end{tablenotes}"_n ///
    "\end{threeparttable}" _n ///
  "\label{tab:lenient_inv_bounds}%" _n ///
"\end{table}%" _n _n _n _n _n 

file close fh
macro drop fh


**************************
**TABLE A7
**************************
*Standard errors for Panel A:
use "${cleandata}analysis_sample_investigators_qje.dta", clear 
gen targeted = categ==2 
replace targeted=. if fc==1

cap drop S_w 
sum targeted if white==1 
gen S_w = r(mean)
local sd = r(sd)
display `sd'
local n = r(N)
display `n'
gen se_S_w = `sd'/sqrt(`n')
sum se_S_w

sum targeted if white==0 
gen S_b = r(mean)
local sd = r(sd)
display `sd'
local n = r(N)
display `n'
gen se_S_b = `sd'/sqrt(`n')
sum se_S_b

sum fc if white==1 
gen D_w = 1 - r(mean)
local sd = r(sd)
display `sd'
local n = r(N)
display `n'
gen se_D_w = `sd'/sqrt(`n')
sum se_D_w

sum fc if white==0 
gen D_b = 1 - r(mean)
local sd = r(sd)
display `sd'
local n = r(N)
display `n'
gen se_D_b = `sd'/sqrt(`n')
sum se_D_b

sum inv6m if white==1 
gen Y_w = r(mean) 
local sd = r(sd)
display `sd'
local n = r(N)
display `n'
gen se_Y_w = `sd'/sqrt(`n')
sum se_Y_w

sum inv6m if white==0 
gen Y_b = r(mean)
local sd = r(sd)
display `sd'
local n = r(N)
display `n'
gen se_Y_b = `sd'/sqrt(`n')
sum se_Y_b

sum bshare 
local bshare = r(mean)

gen count = _n 
keep if count==1
keep Y_w Y_b D_w D_b S_w S_b se_* bshare 
tempfile tmp 
save `tmp', replace 	

forvalues b = 1/500 {
	use `tmp', clear 
foreach x in S_b S_w D_b D_w Y_b Y_w {
	replace `x' = `x' + rnormal(0,se_`x')
}

cap drop hyp_adj_gap
forval pi = 0 (0.1) .6 {	
preserve 	
	
local lb_w = Y_w - `pi'*S_w
local ub_w = Y_w + `pi'*S_w
	
local lb_b = Y_b - `pi'*S_b
local ub_b = Y_b + `pi'*S_b	

	*simulate and plot mean UD estimates, over ATE range
	matrix hyp=J(11000,1,.)
	local row=1
	forval Y_w = `lb_w'(0.01)`ub_w' {
	forval Y_b = `lb_b'(0.01)`ub_b' {
		local ate_lb_b = `Y_b'*D_b 
		local ate_ub_b = `ate_lb_b' + (1-D_b) 
		local ate_lb_w = `Y_w'*D_w 
		local ate_ub_w = `ate_lb_w' + (1-D_w) 	
  		forval ate_b=`ate_lb_b'(0.01)`ate_ub_b' {
  		forval ate_w=`ate_lb_w'(0.01)`ate_ub_w' {
			local ate=`ate_b'*bshare+`ate_w'*(1-bshare)
			gen hyp_adj_gap=((D_w*`Y_w')/`ate_w' ///
				-(D_b*`Y_b')/`ate_b')*`ate' ///
			   +((D_w*(1-`Y_w'))/(1-`ate_w') ///
				-(D_b*(1-`Y_b'))/(1-`ate_b'))*(1-`ate')
			sum hyp_adj_gap 
			matrix hyp[`row',1]=r(mean)
			local ++row
			drop hyp_adj_gap
		}
		}
	}
	}
	clear
	svmat hyp
	local newpi = `pi'*100
	egen min = min(hyp1)
	sum min 
	local delta_lb`newpi' = `r(mean)'
	egen max = max(hyp1)
	sum max 
	local delta_ub`newpi' = `r(mean)'
	restore 
}

gen delta_lb0 = `delta_lb0'
gen delta_lb10 = `delta_lb10'
gen delta_lb20 = `delta_lb20'
gen delta_lb30 = `delta_lb30'
gen delta_lb40 = `delta_lb40'
gen delta_lb50 = `delta_lb50'
gen delta_lb60 = `delta_lb60'

gen delta_ub0 = `delta_ub0'
gen delta_ub10 = `delta_ub10'
gen delta_ub20 = `delta_ub20'
gen delta_ub30 = `delta_ub30'
gen delta_ub40 = `delta_ub40'
gen delta_ub50 = `delta_ub50'
gen delta_ub60 = `delta_ub60'

keep if _n==1
tempfile boot_`b'
save `boot_`b'', replace 
}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	
	
	foreach x in delta_lb0 delta_lb10 delta_lb20 delta_lb30 delta_lb40 delta_lb50 delta_lb60 ///
	delta_ub0 delta_ub10 delta_ub20 delta_ub30 delta_ub40 delta_ub50 delta_ub60 {
		sum `x'
		local se_`x' = string(r(sd), "%10.3fc")
		display `se_`x''
	}

	
*Standard errors for Panel B:
use "${cleandata}analysis_sample_investigators_qje.dta", clear 

gen targeted = categ==2 
replace targeted=. if fc==1

cap drop S_w 
sum targeted if white==1 
gen S_w = r(mean)
local sd = r(sd)
display `sd'
local n = r(N)
display `n'
gen se_S_w = `sd'/sqrt(`n')
sum se_S_w

sum targeted if white==0 
gen S_b = r(mean)
local sd = r(sd)
display `sd'
local n = r(N)
display `n'
gen se_S_b = `sd'/sqrt(`n')
sum se_S_b

sum fc if white==1 
gen D_w = 1 - r(mean)
local sd = r(sd)
display `sd'
local n = r(N)
display `n'
gen se_D_w = `sd'/sqrt(`n')
sum se_D_w

sum fc if white==0 
gen D_b = 1 - r(mean)
local sd = r(sd)
display `sd'
local n = r(N)
display `n'
gen se_D_b = `sd'/sqrt(`n')
sum se_D_b

sum inv6m if white==1 
gen Y_w = r(mean) 
local sd = r(sd)
display `sd'
local n = r(N)
display `n'
gen se_Y_w = `sd'/sqrt(`n')
sum se_Y_w

sum inv6m if white==0 
gen Y_b = r(mean)
local sd = r(sd)
display `sd'
local n = r(N)
display `n'
gen se_Y_b = `sd'/sqrt(`n')
sum se_Y_b

sum bshare 
local bshare = r(mean)

gen count = _n 
keep if count==1
keep Y_w Y_b D_w D_b S_w S_b se_* bshare 
tempfile tmp 
save `tmp', replace 	

forvalues b = 1/500 {
use `tmp', clear 	
foreach x in S_b S_w D_b D_w Y_b Y_w {
	replace `x' = `x' + rnormal(0,se_`x')
}
forvalues tau = 0 (0.01).05 {	
preserve  
local lb_w = Y_w - `tau' 
local ub_w = Y_w + `tau'
	
local lb_b = Y_b - `tau'
local ub_b = Y_b + `tau'

	*simulate and plot mean UD estimates, over ATE range
	matrix hyp=J(11000,1,.)
	local row=1
	forval Y_w = `lb_w'(0.01)`ub_w' {
	forval Y_b = `lb_b'(0.01)`ub_b' {
		local ate_lb_b = `Y_b'*D_b 
		local ate_ub_b = `ate_lb_b' + (1-D_b) 
		local ate_lb_w = `Y_w'*D_w 
		local ate_ub_w = `ate_lb_w' + (1-D_w) 	
  		forval ate_b=`ate_lb_b'(0.01)`ate_ub_b' {
  		forval ate_w=`ate_lb_w'(0.01)`ate_ub_w' {
			local ate=`ate_b'*bshare+`ate_w'*(1-bshare)
			gen hyp_adj_gap=((D_w*`Y_w')/`ate_w' ///
				-(D_b*`Y_b')/`ate_b')*`ate' ///
			   +((D_w*(1-`Y_w'))/(1-`ate_w') ///
				-(D_b*(1-`Y_b'))/(1-`ate_b'))*(1-`ate')
			sum hyp_adj_gap 
			matrix hyp[`row',1]=r(mean)
			local ++row
			drop hyp_adj_gap
		}
		}
	}
	}
	clear
	svmat hyp	
	local newtau = `tau'*100
	egen min = min(hyp1)
	sum min 
	local delta_lb`newtau' = string(`r(mean)', "%10.4fc")
	egen max = max(hyp1)
	sum max 
	local delta_ub`newtau' = string(`r(mean)', "%10.4fc")
	restore 
}

forvalues tau = .06 (0.01).06 {	
preserve  
local lb_w = Y_w - `tau' 
local ub_w = Y_w + `tau'
	
local lb_b = Y_b - `tau'
local ub_b = Y_b + `tau'

	*simulate and plot mean UD estimates, over ATE range
	matrix hyp=J(11000,1,.)
	local row=1
	forval Y_w = `lb_w'(0.01)`ub_w' {
	forval Y_b = `lb_b'(0.01)`ub_b' {
		local ate_lb_b = `Y_b'*D_b 
		local ate_ub_b = `ate_lb_b' + (1-D_b) 
		local ate_lb_w = `Y_w'*D_w 
		local ate_ub_w = `ate_lb_w' + (1-D_w) 	
  		forval ate_b=`ate_lb_b'(0.01)`ate_ub_b' {
  		forval ate_w=`ate_lb_w'(0.01)`ate_ub_w' {
			local ate=`ate_b'*bshare+`ate_w'*(1-bshare)
			gen hyp_adj_gap=((D_w*`Y_w')/`ate_w' ///
				-(D_b*`Y_b')/`ate_b')*`ate' ///
			   +((D_w*(1-`Y_w'))/(1-`ate_w') ///
				-(D_b*(1-`Y_b'))/(1-`ate_b'))*(1-`ate')
			sum hyp_adj_gap 
			matrix hyp[`row',1]=r(mean)
			local ++row
			drop hyp_adj_gap
		}
		}
	}
	}
	clear
	svmat hyp	
	local newtau = `tau'*100
	egen min = min(hyp1)
	sum min 
	local delta_lb`newtau' = r(mean)
	egen max = max(hyp1)
	sum max 
	local delta_ub`newtau' = r(mean)
	restore 
}

gen delta_lb0 = `delta_lb0'
gen delta_lb1 = `delta_lb1'
gen delta_lb2 = `delta_lb2'
gen delta_lb3 = `delta_lb3'
gen delta_lb4 = `delta_lb4'
gen delta_lb5 = `delta_lb5'
gen delta_lb6 = `delta_lb6'

gen delta_ub0 = `delta_ub0'
gen delta_ub1 = `delta_ub1'
gen delta_ub2 = `delta_ub2'
gen delta_ub3 = `delta_ub3'
gen delta_ub4 = `delta_ub4'
gen delta_ub5 = `delta_ub5'
gen delta_ub6 = `delta_ub6'

keep if _n==1
tempfile boot_`b'
save `boot_`b'', replace 
}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	
	
	foreach x in delta_lb0 delta_lb1 delta_lb2 delta_lb3 delta_lb4 delta_lb5 delta_lb6 ///
	delta_ub0 delta_ub1 delta_ub2 delta_ub3 delta_ub4 delta_ub5 delta_ub6 {
		sum `x'
		local se_`x'_noscar = string(r(sd), "%10.3fc")
		display `se_`x'_noscar'
	}


*Bounds for Panel A:
use "${cleandata}analysis_sample_investigators_qje.dta", clear 

gen targeted = categ==2 
replace targeted=. if fc==1

sum targeted if white==1 
gen S_w = r(mean)

sum targeted if white==0 
gen S_b = r(mean)

sum fc if white==1 
gen D_w = 1 - r(mean)

sum fc if white==0 
gen D_b = 1 - r(mean)

sum inv6m if white==1 
gen Y_w = r(mean) 

sum inv6m if white==0 
gen Y_b = r(mean)

sum bshare 
local bshare = r(mean)

gen count = _n 
keep if count==1
	

cap drop hyp_adj_gap
forval pi = 0 (0.1) .6 {	
	preserve  
local lb_w = Y_w - `pi'*S_w 
local ub_w = Y_w + `pi'*S_w
	
local lb_b = Y_b - `pi'*S_b
local ub_b = Y_b + `pi'*S_b	

	*simulate and plot mean UD estimates, over ATE range
	matrix hyp=J(11000,1,.)
	local row=1
	forval Y_w = `lb_w'(0.01)`ub_w' {
	forval Y_b = `lb_b'(0.01)`ub_b' {
		local ate_lb_b = `Y_b'*D_b 
		local ate_ub_b = `ate_lb_b' + (1-D_b) 
		local ate_lb_w = `Y_w'*D_w 
		local ate_ub_w = `ate_lb_w' + (1-D_w) 	
  		forval ate_b=`ate_lb_b'(0.01)`ate_ub_b' {
  		forval ate_w=`ate_lb_w'(0.01)`ate_ub_w' {
			local ate=`ate_b'*bshare+`ate_w'*(1-bshare)
			gen hyp_adj_gap=((D_w*`Y_w')/`ate_w' ///
				-(D_b*`Y_b')/`ate_b')*`ate' ///
			   +((D_w*(1-`Y_w'))/(1-`ate_w') ///
				-(D_b*(1-`Y_b'))/(1-`ate_b'))*(1-`ate')
			sum hyp_adj_gap 
			matrix hyp[`row',1]=r(mean)
			local ++row
			drop hyp_adj_gap
		}
		}
	}
	}
	clear
	svmat hyp
	local newpi = `pi'*100
	egen min = min(hyp1)
	sum min 
	local delta_lb`newpi' = string(`r(mean)', "%10.3fc")
	egen max = max(hyp1)
	sum max 
	local delta_ub`newpi' = string(`r(mean)', "%10.3fc")
	restore 
}

*Bounds for Panel B:
cap drop hyp_adj_gap
forvalues tau = 0 (0.01).05 {	
	
preserve  
local lb_w = Y_w - `tau' 
local ub_w = Y_w + `tau'
	
local lb_b = Y_b - `tau'
local ub_b = Y_b + `tau'

	*simulate and plot mean UD estimates, over ATE range
	matrix hyp=J(11000,1,.)
	local row=1
	forval Y_w = `lb_w'(0.01)`ub_w' {
	forval Y_b = `lb_b'(0.01)`ub_b' {
		local ate_lb_b = `Y_b'*D_b 
		local ate_ub_b = `ate_lb_b' + (1-D_b) 
		local ate_lb_w = `Y_w'*D_w 
		local ate_ub_w = `ate_lb_w' + (1-D_w) 	
  		forval ate_b=`ate_lb_b'(0.01)`ate_ub_b' {
  		forval ate_w=`ate_lb_w'(0.01)`ate_ub_w' {
			local ate=`ate_b'*bshare+`ate_w'*(1-bshare)
			gen hyp_adj_gap=((D_w*`Y_w')/`ate_w' ///
				-(D_b*`Y_b')/`ate_b')*`ate' ///
			   +((D_w*(1-`Y_w'))/(1-`ate_w') ///
				-(D_b*(1-`Y_b'))/(1-`ate_b'))*(1-`ate')
			sum hyp_adj_gap 
			matrix hyp[`row',1]=r(mean)
			local ++row
			drop hyp_adj_gap
		}
		}
	}
	}
	clear
	svmat hyp	
	local newtau = `tau'*100
	egen min = min(hyp1)
	sum min 
	local delta_lb`newtau' = string(`r(mean)', "%10.3fc")
	egen max = max(hyp1)
	sum max 
	local delta_ub`newtau' = string(`r(mean)', "%10.3fc")
	restore 
}

cap drop hyp_adj_gap
forvalues tau = .06 (0.01).06 {	
	
preserve  
local lb_w = Y_w - `tau' 
local ub_w = Y_w + `tau'
	
local lb_b = Y_b - `tau'
local ub_b = Y_b + `tau'

	*simulate and plot mean UD estimates, over ATE range
	matrix hyp=J(11000,1,.)
	local row=1
	forval Y_w = `lb_w'(0.01)`ub_w' {
	forval Y_b = `lb_b'(0.01)`ub_b' {
		local ate_lb_b = `Y_b'*D_b 
		local ate_ub_b = `ate_lb_b' + (1-D_b) 
		local ate_lb_w = `Y_w'*D_w 
		local ate_ub_w = `ate_lb_w' + (1-D_w) 	
  		forval ate_b=`ate_lb_b'(0.01)`ate_ub_b' {
  		forval ate_w=`ate_lb_w'(0.01)`ate_ub_w' {
			local ate=`ate_b'*bshare+`ate_w'*(1-bshare)
			gen hyp_adj_gap=((D_w*`Y_w')/`ate_w' ///
				-(D_b*`Y_b')/`ate_b')*`ate' ///
			   +((D_w*(1-`Y_w'))/(1-`ate_w') ///
				-(D_b*(1-`Y_b'))/(1-`ate_b'))*(1-`ate')
			sum hyp_adj_gap 
			matrix hyp[`row',1]=r(mean)
			local ++row
			drop hyp_adj_gap
		}
		}
	}
	}
	clear
	svmat hyp	
	local newtau = `tau'*100
	egen min = min(hyp1)
	sum min 
	local delta_lb`newtau' = string(`r(mean)', "%10.3fc")
	egen max = max(hyp1)
	sum max 
	local delta_ub`newtau' = string(`r(mean)', "%10.3fc")
	restore 
}

capture file close fh
capture macro drop fh
capture erase "${output}tableA7.tex"
file open fh using "${output}tableA7.tex", write replace
	
	file write fh ///
"\begin{table}[htbp]" _n ///
"\centering" _n ///
"\footnotesize" _n ///
"\caption{Exclusion-Free Bounds of Investigator UD, With and Without Direct Effects}" _n ///
"\begin{threeparttable}" _n ///
"\begin{tabular}{lcccccc}" _n ///
"\hline \hline" _n ///
"   & \textbf{(1)} & \textbf{(2)} & \textbf{(3)} & \textbf{(4)} & \textbf{(5)} & \textbf{(6)} \\" _n ///
"   \cmidrule{2-7}" _n ///
"     & \multicolumn{6}{c}{\textit{Panel A: Without effects from targeted services}} \\" _n ///
"     & \multicolumn{6}{c}{\textbf{Services take-up rate}}   \\" _n ///
"Baseline bounds & 10\%  & 20\%  & 30\%  & 40\%  & 50\%  & 60\% \\" _n ///
"$[`delta_lb0',`delta_ub0']$   & $[`delta_lb10',`delta_ub10']$   & $[`delta_lb20',`delta_ub20']$   & $[`delta_lb30',`delta_ub30']$   & $[`delta_lb40',`delta_ub40']$ &       $[`delta_lb50',`delta_ub50']$ & $[`delta_lb60',`delta_ub60']$ \\ " _n ///
"$(`se_delta_lb0',`se_delta_ub0')$   & $(`se_delta_lb10',`se_delta_ub10')$   & $(`se_delta_lb20',`se_delta_ub20')$   & $(`se_delta_lb30',`se_delta_ub30')$   & $(`se_delta_lb40',`se_delta_ub40')$ &       $(`se_delta_lb50',`se_delta_ub50')$ & $(`se_delta_lb60',`se_delta_ub60')$ \\ \\" _n ///
"      & \multicolumn{6}{c}{\textit{Panel B: Without effects from investigator contact}} \\" _n ///
"       & \multicolumn{6}{c}{\textbf{Range of contact effects}} \\" _n ///
"  Baseline bounds & $\pm1pp$ & $\pm2pp$ & $\pm3pp$ & $\pm4pp$ & $\pm5pp$ & $\pm6pp$ \\" _n ///
"$[`delta_lb0',`delta_ub0']$   & $[`delta_lb1',`delta_ub1']$   & $[`delta_lb2',`delta_ub2']$   & $[`delta_lb3',`delta_ub3']$   & $[`delta_lb4',`delta_ub4']$ & $[`delta_lb5',`delta_ub5']$ & $[`delta_lb6',`delta_ub6']$ \\ " _n ///
"$(`se_delta_lb0_noscar',`se_delta_ub0_noscar')$   & $(`se_delta_lb1_noscar',`se_delta_ub1_noscar')$   & $(`se_delta_lb2_noscar',`se_delta_ub2_noscar')$   & $(`se_delta_lb3_noscar',`se_delta_ub3_noscar')$   & $(`se_delta_lb4_noscar',`se_delta_ub4_noscar')$ &       $(`se_delta_lb5_noscar',`se_delta_ub5_noscar')$ & $(`se_delta_lb6_noscar',`se_delta_ub6_noscar')$ \\ \\" _n 

	file write fh ///
"\hline \hline " _n ///
"\end{tabular}%" _n ///
"\begin{tablenotes}" _n ///
"\item \footnotesize \textit{Notes.} Panel A reports the exclusion-free bounds described in Section \ref{sect:exclusion} and formalized in Online Appendix \ref{apx:exclusion}, as well as the extension which removes any possible direct effects of targeted services. Panel B similarly reports the exclusion-free bounds and the extension which removes any possible unobserved contact effects. Robust standard errors on the endpoints of each set of bounds, two-way-clustered at the child and investigator level, are obtained by a bootstrapping procedure (with 500 replications) and appear in parentheses." _n /// 
"\end{tablenotes}" _n ///
"\end{threeparttable} " _n ///
"  \label{tab:probing_exclusion}%" _n ///  
"\end{table}%" _n _n _n _n _n 

file close fh
macro drop fh



**************************
**TABLE A8
**************************
capture file close fh
capture macro drop fh
capture erase "${output}tableA8.tex"
file open fh using "${output}tableA8.tex", write replace

*Compute conditional placement rates among all calls:
 
*(1) Estimate screen-in rates
*First need to estimate race-specific ATEs among all calls 
use "${cleandata}screener_placement_maltreatment_rates_se_qje.dta", clear 
	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_fe_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin=`r(mean)'
			local `r'_ate_lin=`r(mean)'
	}
	
*Now, estimate conditional screen-in rates 
use "${cleandata}screener_screening_maltreatment_rates_qje.dta", clear 
rename (b_screened w_screened b_inv6m w_inv6m) (D_b D_w Y_b Y_w)
foreach x in D_b D_w {
	replace `x' = 1-`x'
}
	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
*ATEs are obtained via extrapolation above:
gen ate_w = `w_ate_lin' 
gen ate_b = `b_ate_lin' 
gen ate = ate_w * (1-bshare) + ate_b *(bshare)
gen delta1_w_screen= (D_w*Y_w)/ate_w
gen delta1_b_screen=(D_b*Y_b)/ate_b 
gen delta0_w_screen = (D_w*(1-Y_w))/(1-ate_w) 
gen delta0_b_screen = (D_b*(1-Y_b))/(1-ate_b) 
gen delta_b_screen = delta1_b_screen*`b_ate_lin' + delta0_b_screen*(1-`b_ate_lin')
gen delta_w_screen = delta1_w_screen*`w_ate_lin' + delta0_w_screen*(1-`w_ate_lin')

foreach x in delta1_w_screen delta1_b_screen delta0_w_screen delta0_b_screen delta_b_screen delta_w_screen {
		replace `x' = 1 - `x'
		sum `x' [aw=count_inv]
		local `x' = r(mean)
	}

*(2) Estimate placement rates among screened-in calls 
	use "${cleandata}inv_adjusted_rates_se_omitted_qje.dta", clear
	sum bshare 
	local bshare = r(mean)
	
	rename (w_nofc b_nofc w_inv6m b_inv6m) (D_w D_b Y_w Y_b)
	rename (w_se_nofc b_se_nofc) (w_se_fe_nofc b_se_fe_nofc)
	
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	
	*Need race-specific ATEs among screened-in calls 
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100
			
			*Linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}

		*Generate delta j's:
		gen delta1_w_inv=(D_w*Y_w)/`w_ate_lin'
		gen delta1_b_inv=(D_b*Y_b)/`b_ate_lin' 
		gen delta0_w_inv=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen delta0_b_inv=(D_b*(1-Y_b))/(1-`b_ate_lin')
		gen delta_w_inv = delta1_w_inv*`w_ate_lin' + delta0_w_inv*(1-`w_ate_lin')
		gen delta_b_inv = delta1_b_inv*`b_ate_lin' + delta0_b_inv*(1-`b_ate_lin')
		
		foreach x in delta1_w_inv delta1_b_inv delta0_w_inv delta0_b_inv delta_w_inv delta_b_inv {
			replace `x' = 1 - `x'
			sum `x' [aw=count_inv]
			local `x' = r(mean)
	}
	
*Generate placement rate in the full population of calls 
foreach x in delta_w delta1_w delta0_w delta_b delta1_b delta0_b {
	local `x'_all = string(``x'_screen' * ``x'_inv', "%10.3fc")	
	display ``x'_all'
}

*Boostrapped standard errors for conditional placement rates among all calls: 
forvalues b = 1/500 {
*(1) Estimate screen-in rates 
*Need race-specific ATEs among all calls
use "${cleandata}screener_placement_maltreatment_rates_se_qje.dta", clear 
count
local n_screeners = r(N)

	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
	replace D_b = D_b + rnormal(0,b_twse_fc)
	replace D_w = D_w + rnormal(0,w_twse_fc)
	replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
	replace Y_w = Y_w + rnormal(0,w_twse_inv6m)
	
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_fe_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin=`r(mean)'
			local `r'_ate_lin=`r(mean)'
	}
	
*Now, using the ATEs above, compute screen-in rates 
use "${cleandata}screener_screening_maltreatment_rates_qje.dta", clear 
rename (b_screened w_screened b_inv6m w_inv6m) (D_b D_w Y_b Y_w)
foreach x in D_b D_w {
	replace `x' = 1-`x'
}
	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
*ATEs are obtained via extrapolation above:
gen ate_w = `w_ate_lin' 
gen ate_b = `b_ate_lin' 
gen ate = ate_w * (1-bshare) + ate_b *(bshare)
gen delta1_w_screen= (D_w*Y_w)/ate_w
gen delta1_b_screen=(D_b*Y_b)/ate_b 
gen delta0_w_screen = (D_w*(1-Y_w))/(1-ate_w) 
gen delta0_b_screen = (D_b*(1-Y_b))/(1-ate_b) 
gen delta_b_screen = delta1_b_screen*`b_ate_lin' + delta0_b_screen*(1-`b_ate_lin')
gen delta_w_screen = delta1_w_screen*`w_ate_lin' + delta0_w_screen*(1-`w_ate_lin')

foreach x in delta1_w_screen delta1_b_screen delta0_w_screen delta0_b_screen delta_b_screen delta_w_screen {
		replace `x' = 1 - `x'
		sum `x' [aw=count_inv]
		local `x' = r(mean)
	}


*(2) Next, estimate placement rates among screened-in calls 
	use "${cleandata}inv_adjusted_rates_se_omitted_qje.dta", clear
	sum bshare 
	local bshare = r(mean)
	
	rename (w_nofc b_nofc w_inv6m b_inv6m) (D_w D_b Y_w Y_b)
	rename (w_twse_nofc b_twse_nofc) (w_se_fe_nofc b_se_fe_nofc)
	
	replace D_b = D_b + rnormal(0,b_se_fe_nofc)
	replace D_w = D_w + rnormal(0,w_se_fe_nofc)
	replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
	replace Y_w = Y_w + rnormal(0,w_twse_inv6m)
	
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	
	*Estimate race-specific ATEs among screened-in calls  
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100
			
			*Linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}

		*Generate delta j's:
		gen delta1_w_inv=(D_w*Y_w)/`w_ate_lin'
		gen delta1_b_inv=(D_b*Y_b)/`b_ate_lin' 
		gen delta0_w_inv=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen delta0_b_inv=(D_b*(1-Y_b))/(1-`b_ate_lin')
		gen delta_w_inv = delta1_w_inv*`w_ate_lin' + delta0_w_inv*(1-`w_ate_lin')
		gen delta_b_inv = delta1_b_inv*`b_ate_lin' + delta0_b_inv*(1-`b_ate_lin')
		
		foreach x in delta1_w_inv delta1_b_inv delta0_w_inv delta0_b_inv delta_w_inv delta_b_inv {
			replace `x' = 1 - `x'
			sum `x' [aw=count_inv]
			local `x' = r(mean)
	}
	
*Generate placement rate in the full population of calls 
foreach x in delta_w delta1_w delta0_w delta_b delta1_b delta0_b {
	local `x'_full = string(``x'_screen' * ``x'_inv', "%10.3fc")
	gen `x'_full = ``x'_full'
}

keep if _n==1 
keep delta_w_full delta1_w_full delta0_w_full delta_b_full delta1_b_full delta0_b_full

			tempfile boot_`b'
			save `boot_`b'', replace	

}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	

		foreach x in delta_w_full delta1_w_full delta0_w_full delta_b_full delta1_b_full delta0_b_full {
			sum `x'
			local se_`x' = string(r(sd), "%10.3fc")	
			display `se_`x''
		}


*Next, compute and save conditional screen-in rates, as above:
use "${cleandata}screener_placement_maltreatment_rates_se_qje.dta", clear 
count
local n_screeners = r(N)

	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_fe_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin=`r(mean)'
			local `r'_ate_lin=`r(mean)'
	}
			
use "${cleandata}screener_screening_maltreatment_rates_qje.dta", clear 
rename (b_screened w_screened b_inv6m w_inv6m) (D_b D_w Y_b Y_w)
foreach x in D_b D_w {
	replace `x' = 1-`x'
}
	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
*ATEs are obtained via extrapolation above:
gen ate_w = `w_ate_lin' 
gen ate_b = `b_ate_lin' 
gen ate = ate_w * (1-bshare) + ate_b *(bshare)
gen delta1_w= (D_w*Y_w)/ate_w
gen delta1_b=(D_b*Y_b)/ate_b 
gen delta0_w = (D_w*(1-Y_w))/(1-ate_w) 
gen delta0_b = (D_b*(1-Y_b))/(1-ate_b) 
gen delta_b = delta1_b*`b_ate_lin' + delta0_b*(1-`b_ate_lin')
gen delta_w = delta1_w*`w_ate_lin' + delta0_w*(1-`w_ate_lin')

foreach x in delta1_w delta1_b delta0_w delta0_b delta_b delta_w {
	replace `x' = 1 - `x'
}

foreach x in delta1_w delta1_b delta0_w delta0_b delta_b delta_w {
	sum `x' [aw=count_inv]
	local `x'_screen = string(`r(mean)', "%10.3fc")		
	display ``x'_screen'
}

*SEs for these conditional screen-in rates: 
forvalues b = 1/500 {
use "${cleandata}screener_placement_maltreatment_rates_se_qje.dta", clear 
	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'

	replace D_b = D_b + rnormal(0,b_twse_fc)
	replace D_w = D_w + rnormal(0,w_twse_fc)
	replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
	replace Y_w = Y_w + rnormal(0,w_twse_inv6m)
	
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_fe_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin=`r(mean)'
			local `r'_ate_lin=`r(mean)'
	}
			
use "${cleandata}screener_screening_maltreatment_rates_qje.dta", clear 
rename (b_screened w_screened b_inv6m w_inv6m) (D_b D_w Y_b Y_w)

foreach x in D_b D_w {
	replace `x' = 1-`x'
}
	gen inv_bshare = (count_black/count_inv)
	sum inv_bshare [aw=count_inv] 
	gen bshare = `r(mean)'
	local bshare = `r(mean)'
	
*ATEs are obtained via extrapolation above:
gen ate_w = `w_ate_lin' 
gen ate_b = `b_ate_lin' 
gen ate = ate_w * (1-bshare) + ate_b *(bshare)
gen delta1_w= (D_w*Y_w)/ate_w
gen delta1_b=(D_b*Y_b)/ate_b 
gen delta0_w = (D_w*(1-Y_w))/(1-ate_w) 
gen delta0_b = (D_b*(1-Y_b))/(1-ate_b) 
gen delta_b = delta1_b*`b_ate_lin' + delta0_b*(1-`b_ate_lin')
gen delta_w = delta1_w*`w_ate_lin' + delta0_w*(1-`w_ate_lin')

foreach x in delta1_w delta1_b delta0_w delta0_b delta_b delta_w {
	replace `x' = 1 - `x'
}

foreach x in delta1_w delta1_b delta0_w delta0_b delta_b delta_w {
	sum `x' [aw=count_inv]
	gen `x'_boot = r(mean)	
}

	keep if _n==1
	
			tempfile boot_`b'
			save `boot_`b'', replace	
}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	

		foreach x in delta1_w delta1_b delta0_w delta0_b delta_b delta_w {
			sum `x'_boot
			local se_`x'_screen = string(r(sd), "%10.3fc")	
			display `se_`x'_screen'
		}



*Finally, compute conditional placement rates among screened-in calls and standard errors:
	use "${cleandata}inv_adjusted_rates_se_omitted_qje.dta", clear
	count 
	local n_inv = r(N)
	
	sum bshare 
	local bshare = r(mean)
	
	rename (w_nofc b_nofc w_inv6m b_inv6m) (D_w D_b Y_w Y_b)
	rename (w_se_nofc b_se_nofc) (w_se_fe_nofc b_se_fe_nofc)
	
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	 
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100
			
			*Linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}

		*Generate delta j's:
		gen delta1_w_inv=(D_w*Y_w)/`w_ate_lin'
		gen delta1_b_inv=(D_b*Y_b)/`b_ate_lin' 
		gen delta0_w_inv=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen delta0_b_inv=(D_b*(1-Y_b))/(1-`b_ate_lin')
		gen delta_w_inv = delta1_w_inv*`w_ate_lin' + delta0_w_inv*(1-`w_ate_lin')
		gen delta_b_inv = delta1_b_inv*`b_ate_lin' + delta0_b_inv*(1-`b_ate_lin')
		
		foreach x in delta1_w_inv delta1_b_inv delta0_w_inv delta0_b_inv delta_w_inv delta_b_inv {
			replace `x' = 1 - `x'
		}
		
	// case-weighted mean
	foreach type in delta1_w_inv delta1_b_inv delta0_w_inv delta0_b_inv delta_w_inv delta_b_inv  {
		sum `type' [aw=count_inv]
		local `type' = string(`r(mean)', "%10.3fc")
	}

*Standard errors for conditional placement rates: 
forvalues b = 1/500 {
	use "${cleandata}inv_adjusted_rates_se_omitted_qje.dta", clear
	sum bshare 
	local bshare = r(mean)
	
	rename (w_nofc b_nofc w_inv6m b_inv6m) (D_w D_b Y_w Y_b)
	rename (w_twse_nofc b_twse_nofc) (w_se_fe_nofc b_se_fe_nofc)
	
	replace D_b = D_b + rnormal(0,b_se_fe_nofc)
	replace D_w = D_w + rnormal(0,w_se_fe_nofc)
	replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
	replace Y_w = Y_w + rnormal(0,w_twse_inv6m)
			
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100
			
			*Linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}

		*Generate delta j's:
		gen delta1_w_inv=(D_w*Y_w)/`w_ate_lin'
		gen delta1_b_inv=(D_b*Y_b)/`b_ate_lin' 
		gen delta0_w_inv=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen delta0_b_inv=(D_b*(1-Y_b))/(1-`b_ate_lin')
		gen delta_w_inv = delta1_w_inv*`w_ate_lin' + delta0_w_inv*(1-`w_ate_lin')
		gen delta_b_inv = delta1_b_inv*`b_ate_lin' + delta0_b_inv*(1-`b_ate_lin')
		
		foreach x in delta1_w_inv delta1_b_inv delta0_w_inv delta0_b_inv delta_b_inv delta_w_inv {
			replace `x' = 1 - `x'
		}
		
	// case-weighted mean
	foreach type in delta1_w_inv delta1_b_inv delta0_w_inv delta0_b_inv delta_b_inv delta_w_inv  {
		sum `type' [aw=count_inv]
		replace `type' = r(mean)
	}
	
	keep if _n==1
	
			tempfile boot_`b'
			save `boot_`b'', replace	
		}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	

		foreach x in delta1_w_inv delta1_b_inv delta0_w_inv delta0_b_inv delta_b_inv delta_w_inv {
			sum `x'
			local se_`x' = string(r(sd), "%10.3fc")	
			display `se_`x''
		}
		

		file write fh ///
	"\begin{table}[htbp]" _n ///
	"\footnotesize"  _n ///
  "\centering" _n ///
  "\caption{Estimates of Screen-in and Placement Rates by Subsequent Maltreatment Potential}" _n ///
      "\begin{threeparttable}" _n ///
   " \begin{tabular}{lccc}" _n ///
		      "\hline \hline " _n ///
          "& \textbf{(1)} & \textbf{(2)} & \textbf{(3)} \\" _n ///
          "& \textbf{Total} & \textbf{With } & \textbf{Without } \\" _n ///
          "&       & \textbf{maltreatment} & \textbf{maltreatment} \\" _n ///
          "&       & \textbf{potential} & \textbf{potential} \\" _n ///
          "\cmidrule{2-4}" _n ///
          "& \multicolumn{3}{c}{\textit{Panel A: Conditional screen-in rates}} \\" _n ///
    "Black children & `delta_b_screen'      & `delta1_b_screen'    & `delta0_b_screen'  \\" _n ///
     "     			& (`se_delta_b_screen')      & (`se_delta1_b_screen')       & (`se_delta0_b_screen') \\" _n ///
    "White children &  `delta_w_screen'     & `delta1_w_screen'      & `delta0_w_screen'  \\" _n ///
     "     			& (`se_delta_w_screen')      & (`se_delta1_w_screen')       & (`se_delta0_w_screen') \\ \\" _n ///
     "     & \multicolumn{3}{c}{\textit{Panel B: Placement rates among screened-in calls}} \\" _n ///
    "Black children & `delta_b_inv'      & `delta1_b_inv'      & `delta0_b_inv'  \\" _n ///
	"			   & (`se_delta_b_inv') & (`se_delta1_b_inv')  & (`se_delta0_b_inv')  \\" _n ///
    "White children & `delta_w_inv'      & `delta1_w_inv'      & `delta0_w_inv'  \\" _n ///
		"		   & (`se_delta_w_inv') & (`se_delta1_w_inv')  & (`se_delta0_w_inv')  \\ \\" _n ///
     "     & \multicolumn{3}{c}{\textit{Panel C: Placement rates among all calls}} \\" _n ///
    "Black children & `delta_b_all'      & `delta1_b_all'      & `delta0_b_all'  \\" _n ///
	"			   & (`se_delta_b_full') & (`se_delta1_b_full')  & (`se_delta0_b_full')  \\" _n ///
    "White children & `delta_w_all'      & `delta1_w_all'      & `delta0_w_all'  \\" _n ///
		"		   & (`se_delta_w_full') & (`se_delta1_w_full')  & (`se_delta0_w_full')  \\" _n ///
	"&       &       &  \\" _n ///
    "Number of screeners & `n_screeners'      & `n_screeners'      & `n_screeners' \\" _n ///
    "Number of investigators &   `n_inv'    &  `n_inv'   & `n_inv'  \\" _n 
	
	 file write fh ///
	"\hline \hline" _n /// 
	"   \end{tabular}%" _n ///
	"\begin{tablenotes}" _n ///
   " \item \footnotesize \textit{Notes.} This table presents estimates of conditional treatment rates in each phase of the CPS system. Panel A presents estimates of $E[S_i|R_i=r,Y^*_i=y]$. Column 1 presents these estimates averaged across cases with and without maltreatment potential (weighted by the relevant $\mu$). Column 2 focuses on cases with maltreatment potential ($E[S_i|R_i=r,Y^*_i=1]$), while Column 3 focuses on cases without maltreatment potential ($E[S_i|R_i=r,Y^*_i=0]$). Panels B and C repeat this exercise for $E[D_i|R_i=r,Y^*_i=y,S_i=1]$ and $E[P_i|R_i=r,Y^*_i=y]$, respectively. Panel A presents robust standard errors two-way clustered at the child and screener level. Robust standard errors in Panel B are two-way clustered at the child and investigator level. Robust standard errors for estimates in Panel C reflect uncertainty in the underlying estimates in panels A and B. All standard errors are obtained via a bootstrapping procedure (with 500 replications) and appear in parentheses." _n ///
	"\end{tablenotes}" _n ///
	"    \end{threeparttable}" _n ///
	"  \label{tab:cond_rates}%" _n ///
	"\end{table}%" _n _n _n _n _n 

file close fh
macro drop fh


**************************
**TABLE A9
**************************
capture file close fh
capture macro drop fh
capture erase "${output}tableA9.tex"
file open fh using "${output}tableA9.tex", write replace

	use  "${cleandata}limited_inv_analysis_sample.dta", clear
	count 
	local n_inv = r(N)
	
	rename (w_fe_fc b_fe_fc w_fe_inv6m b_fe_inv6m) (D_w D_b Y_w Y_b)
	rename (w_se_fe_fc b_se_fe_fc) (w_se_fe_nofc b_se_fe_nofc)
		
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	sum bshare 
	local bshare = `r(mean)'
	

	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_fe_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}
	
	foreach type in lin {
			sum w_ate_`type'
			local txt_w_ate_`type' = string(r(mean), "%10.3fc")
			
			sum b_ate_`type'
			local txt_b_ate_`type' = string(r(mean), "%10.3fc")
			
		}
		
		*Generate delta j's:
		*Linear:
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen beta_gap0_lin_w=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen beta_gap0_lin_b=(D_b*(1-Y_b))/(1-`b_ate_lin')
		
		gen ate_lin =  bshare*`b_ate_lin' + (1-bshare)*`w_ate_lin'
		
		gen beta_gap_lin = (beta_gap0_lin_w * (1 - ate_lin) + beta_gap1_lin_w *(ate_lin))  - ///
		(beta_gap0_lin_b * (1 - ate_lin) + beta_gap1_lin_b * (ate_lin))
		
		
	// case-weighted mean
	foreach type in lin {
	
		sum beta_gap_`type' [aw=count_inv]
		local `type'_m_text_case = string(`r(mean)', "%10.3fc")
		display "`type'_m_text_case"
	}
	
	*Now, compute bootstrapped standard errors:
	use  "${cleandata}limited_inv_analysis_sample.dta", clear

	rename (w_fe_fc b_fe_fc w_fe_inv6m b_fe_inv6m) (D_w D_b Y_w Y_b)
	rename (w_se_tw_fe_fc b_se_tw_fe_fc) (w_se_fe_nofc b_se_fe_nofc)
		
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	sum bshare 
	local bshare = `r(mean)'
		
	
	// bootstrapped standard errors
		tempfile temp
		save `temp', replace		
		forvalues b = 1/500 {

			use `temp', replace
			
			*generate bootstrap first estimates
			replace D_b = D_b + rnormal(0,b_se_fe_nofc)
			replace D_w = D_w + rnormal(0,w_se_fe_nofc)
			replace Y_b = Y_b + rnormal(0,b_se_tw_fe_inv6m)
			replace Y_w = Y_w + rnormal(0,w_se_tw_fe_inv6m)
	
			foreach r in b w {
			
				*inverse variance of the investigator FE
				gen `r'_var=1/(`r'_se_fe_inv6m^2)
				
				*grid to plot fitted values
				sum D_`r'
				range `r'_points `r(min)' 1 100

				*linear regression
				reg Y_`r' D_`r' [aw=`r'_var]
				local ate_`r'_lin=_b[_cons]+_b[D_`r']				
			
			}	
			local ate_lin=`ate_b_lin'*`bshare'+`ate_w_lin'*(1-`bshare')
		
		*Gen UDs for this boot rep  
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen beta_gap0_lin_w=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen beta_gap0_lin_b=(D_b*(1-Y_b))/(1-`b_ate_lin')
		gen ate_lin =  bshare*`b_ate_lin' + (1-bshare)*`w_ate_lin'

		gen ud_boot_lin = (beta_gap0_lin_w * (1 - ate_lin) + beta_gap1_lin_w *(ate_lin))  - ///
		(beta_gap0_lin_b * (1 - ate_lin) + beta_gap1_lin_b * (ate_lin))			
				
			local index = 1
			// delta 
			foreach type in lin {
				sum ud_boot_`type' [aw=count_inv]
				gen avg_ud_boot_`type'_case = `r(mean)' if _n==1		
			}
			
			keep avg_*
			keep if _n==1
			foreach r in "w" "b" {
				gen ate_`r'_lin=`ate_`r'_lin'
			}
			
			tempfile boot_`b'
			save `boot_`b'', replace	
		}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	
	
		local index =1 			
		foreach type in lin {
			sum ate_w_`type'
			local se_w_ate_`type' = string(r(sd), "%10.3fc")
			
			sum ate_b_`type'
			local se_b_ate_`type' = string(r(sd), "%10.3fc")
			
			sum avg_ud_boot_`type'_case
			local se_m_`index'_case = string(r(sd), "%10.3fc")
						
			local ++index
		}
	
		// make table
		file write fh ///
"\begin{table}[htbp]" _n ///
  "\centering" _n ///
  "\footnotesize" _n ///
  "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
  "\caption{Estimates of Maltreatment Risk and Investigator UD (2008-2017)}" _n ///
  "\begin{threeparttable}" _n ///
   "\begin{tabular}{lcc} " _n ///
    "\hline \hline " _n ///
	"& \textbf{(1)} & \textbf{(2)} \\ " _n ///	
	"& \textbf{Baseline} & \textbf{2008-2017} \\"_n ///
	"\cmidrule{2-3}"_n ///
         " & \multicolumn{2}{c}{\textit{Panel A: Subsequent maltreatment risk}} \\" _n ///
    "Black children & 0.155	& `txt_b_ate_lin' \\" _n ///
	"				& (0.003)	& (`se_b_ate_lin') \\" _n ///
    "White children & 0.175		&  `txt_w_ate_lin' \\" _n ///
	"				& (0.003)		& (`se_w_ate_lin') \\ \\ " _n ///
	"				& \multicolumn{2}{c}{\textit{Panel B: Investigator UD}} \\" _n ///
    "Mean across investigators & 0.017	&  `lin_m_text_case' \\" _n ///
	"							  &	 (0.002)   & (`se_m_1_case') \\" _n ///
	"							&  \\" _n ///
    "Number of investigators & 814 & `n_inv' \\ " _n
   
   file write fh ///
        "\hline \hline " _n ///
    "\end{tabular}%" _n ///
  "\begin{tablenotes}" _n ///
    "\item \footnotesize \textit{Notes.} This table summarizes estimates of mean maltreatment risk among screened-in calls and average (case-weighted) investigator UD. Column 2 presents estimates from the sample of screened-in investigations from 2008--2017, for which we have information on investigators' demographic information. Panel A reports estimates of race-specific average re-investigation risk, while Panel B reports estimates of average investigator UD. Estimates in Panel A come from a linear extrapolation of the variation in Panel B of Figure \ref{fig:extrapolation}. Robust standard errors, two-way clustered at the child and investigator levels, are obtained by a bootstrapping procedure (with 500 replications) and appear in parentheses." _n ///
"\end{tablenotes}" _n ///
   " \end{threeparttable}" _n ///
  "\label{tab:main_inv_2017}%" _n ///
"\end{table}% " _n _n _n _n _n 

file close fh 
macro drop fh

**************************
**TABLE A10
**************************
capture file close fh
capture macro drop fh
capture erase "${output}tableA10.tex"
file open fh using "${output}tableA10.tex", write replace
	
	use "${cleandata}inv_rates_se_covariates_qje.dta", clear
	count 
	local n_inv = r(N)

	sum bshare 
	local bshare = `r(mean)'
	

	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}
	
	foreach type in lin {
			sum w_ate_`type'
			local txt_w_ate_`type' = string(r(mean), "%10.3fc")
			
			sum b_ate_`type'
			local txt_b_ate_`type' = string(r(mean), "%10.3fc")
			
		}
		
		*Generate delta j's:
		*Linear:
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen beta_gap0_lin_w=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen beta_gap0_lin_b=(D_b*(1-Y_b))/(1-`b_ate_lin')
		
		gen ate_lin =  bshare*`b_ate_lin' + (1-bshare)*`w_ate_lin'
		
		gen beta_gap_lin = (beta_gap1_lin_w - beta_gap1_lin_b)*ate_lin + (beta_gap0_lin_w - beta_gap0_lin_b)*(1-ate_lin)
		
	// case-weighted mean
	foreach type in lin {
	
		sum beta_gap_`type' [aw=count_inv]
		local `type'_m_text_case = string(`r(mean)', "%10.3fc")
		display "`type'_m_text_case"
	}
	
	*Bootstrap standard errors 
	use "${cleandata}inv_rates_se_covariates_qje.dta", clear
	sum bshare 
	local bshare = `r(mean)'
	
	// bootstrapped standard errors
		tempfile temp
		save `temp', replace		
		forvalues b = 1/500 {

			use `temp', replace
			
			*generate bootstrap first estimates
			replace D_b = D_b + rnormal(0,b_twse_fc)
			replace D_w = D_w + rnormal(0,w_twse_fc)
			replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
			replace Y_w = Y_w + rnormal(0,w_twse_inv6m)
	
			foreach r in b w {
			
				*inverse variance of the investigator FE
				gen `r'_var=1/(`r'_se_inv6m^2)
				
				*grid to plot fitted values
				sum D_`r'
				range `r'_points `r(min)' 1 100

				*linear regression
				reg Y_`r' D_`r' [aw=`r'_var]
				local ate_`r'_lin=_b[_cons]+_b[D_`r']
			
			}
				
			local ate_lin=`ate_b_lin'*`bshare'+`ate_w_lin'*(1-`bshare')
			
		*Gen pieces needed for each UD 
		*Linear:
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen beta_gap0_lin_w=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen beta_gap0_lin_b=(D_b*(1-Y_b))/(1-`b_ate_lin')
		gen ate_lin =  bshare*`b_ate_lin' + (1-bshare)*`w_ate_lin'

			// generate UDs
		gen ud_boot_lin = (beta_gap1_lin_w - beta_gap1_lin_b)*(ate_lin)  + ///
		(beta_gap0_lin_w - beta_gap0_lin_b)*(1 - ate_lin)
				
			local index = 1
			// delta 
			foreach type in lin {
				sum ud_boot_`type' [aw=count_inv]
				gen avg_ud_boot_`type'_case = `r(mean)' if _n==1
			
			}
			
			keep avg_*
			keep if _n==1
			foreach r in "w" "b" {
				gen ate_`r'_lin=`ate_`r'_lin'
			}
			
			tempfile boot_`b'
			save `boot_`b'', replace	
		}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	
	
		local index =1 			
		foreach type in lin {
			sum ate_w_`type'
			local se_w_ate_`type' = string(r(sd), "%10.3fc")
			
			sum ate_b_`type'
			local se_b_ate_`type' = string(r(sd), "%10.3fc")
			
			sum avg_ud_boot_`type'_case
			local se_m_`index'_case = string(r(sd), "%10.3fc")
			
			local ++index
		}
		
		// make table
		file write fh ///
"\begin{table}[htbp]" _n ///
  "\centering" _n ///
  "\footnotesize" _n ///
  "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
  "\caption{Covariate-Adjusted Estimates of Maltreatment Risk and Investigator UD}" _n ///
  "\begin{threeparttable}" _n ///
   "\begin{tabular}{lcc} " _n ///
    "\hline \hline " _n ///
	"& \textbf{(1)} & \textbf{(2)} \\ " _n ///	
	"& \textbf{Baseline} & \textbf{Covariate-Adjusted} \\"_n ///
	"\cmidrule{2-3}"_n ///
         " & \multicolumn{2}{c}{\textit{Panel A: Subsequent maltreatment risk}} \\" _n ///
    "Black children & 0.155			& `txt_b_ate_lin' \\" _n ///
	"				& (0.003)		& (`se_b_ate_lin') \\" _n ///
    "White children & 0.175			&  `txt_w_ate_lin' \\" _n ///
	"				& (0.003)		& (`se_w_ate_lin') \\ \\ " _n ///
	"				& \multicolumn{2}{c}{\textit{Panel B: Investigator UD}} \\" _n ///
    "Mean across investigators & 0.017	&  `lin_m_text_case' \\" _n ///
	"							  &	 (0.002)   & (`se_m_1_case') \\" _n ///
	"							&  \\" _n ///
    "Number of investigators & 814 & `n_inv' \\ " _n
   
   file write fh ///
        "\hline \hline " _n ///
    "\end{tabular}%" _n ///
  "\begin{tablenotes}" _n ///
    "\item \footnotesize \textit{Notes}. This table summarizes estimates of mean maltreatment risk among screened-in calls and average (case-weighted) investigator UD for different re-investigation time horizons. Panel A reports estimates of race-specific average re-investigation risk, while Panel B reports estimates of average investigator UD. Estimates in Panel A come from a linear extrapolation of the variation in Panel B of Figure \ref{fig:extrapolation}, but where placement and re-investigation rates adjust for the covariates in Column 4 of Table \ref{tab:sumstats} in addition to ZIP code by year fixed effects. Robust standard errors, two-way clustered at the child and investigator levels, are obtained by a bootstrapping procedure (with 500 replications) and appear in parentheses." _n ///
"\end{tablenotes}" _n ///
   " \end{threeparttable}" _n ///
  "\label{tab:main_inv_cov}%" _n ///
"\end{table}% " _n _n _n _n _n 

file close fh 
macro drop fh



**************************
**TABLE A11
**************************
capture file close fh
capture macro drop fh
capture erase "${output}tableA11.tex"
file open fh using "${output}tableA11.tex", write replace
	
	foreach var in male female young old abuse neg {
	use "${cleandata}inv_rates_se_`var'_qje.dta", clear
	count 
	local n_`var' = r(N)
	
	sum bshare 
	local bshare = r(mean)
	
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_fe_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100
			
			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}

	foreach type in lin {
			sum w_ate_`type'
			local `var'_w_ate_`type' = string(r(mean), "%10.3fc")
			
			sum b_ate_`type'
			local `var'_b_ate_`type' = string(r(mean), "%10.3fc")
			
		}
		
		*Generate delta j's:
		gen ate_lin =  bshare*`b_ate_lin' + (1-bshare)*`w_ate_lin'
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen beta_gap0_lin_w=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen beta_gap0_lin_b=(D_b*(1-Y_b))/(1-`b_ate_lin')
		
		gen beta_gap_lin = (beta_gap1_lin_w - beta_gap1_lin_b)*(ate_lin) + ///
		(beta_gap0_lin_w - beta_gap0_lin_b)*(1-ate_lin)
		

	// case-weighted mean
	foreach type in lin {
	
		sum beta_gap_`type' [aw=count_inv]
		local `var'_`type'_mn_text_case = string(`r(mean)', "%10.3fc")
	}
	
	use "${cleandata}inv_rates_se_`var'_qje.dta", clear
	sum bshare 
	local bshare = `r(mean)'
	
	// bootstrapped standard errors
		tempfile temp
		save `temp', replace		
		forvalues b = 1/500 {

			use `temp', replace
			
			*generate bootstrap first estimates
			replace D_b = D_b + rnormal(0,b_twse_fe_fc)
			replace D_w = D_w + rnormal(0,w_twse_fe_fc)
			replace Y_b = Y_b + rnormal(0,b_twse_fe_inv6m)
			replace Y_w = Y_w + rnormal(0,w_twse_fe_inv6m)
	
			foreach r in b w {
			
				*inverse variance of the investigator FE
				gen `r'_var=1/(`r'_se_fe_inv6m^2)
				
				*grid to plot fitted values
				replace D_`r'=1 if D_`r'>1
				sum D_`r'
				range `r'_points `r(min)' 1 100
				
				*linear regression
				reg Y_`r' D_`r' [aw=`r'_var]
				gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
				sum `r'_fitted_1 if `r'_points==1
				gen ate_`r'_lin = `r(mean)'
				local ate_`r'_lin=`r(mean)'
			}
			local ate_lin=`ate_b_lin'*`bshare'+`ate_w_lin'*(1-`bshare')
				
			// generate UDs
			gen ud_boot_lin=((D_w*Y_w)/`ate_w_lin' ///
				-(D_b*Y_b)/`ate_b_lin')*`ate_lin' ///
			   +((D_w*(1-Y_w))/(1-`ate_w_lin') ///
				-(D_b*(1-Y_b))/(1-`ate_b_lin'))*(1-`ate_lin') 
			
			local index = 1
			// case weighted means 
			foreach type in lin {
				sum ud_boot_`type' [aw=count_inv]
				gen avg_ud_boot_`type'_case = `r(mean)' if _n==1
			
			}
			
			keep avg_*
			keep if _n==1
			foreach r in "w" "b" {
				gen ate_`r'_lin=`ate_`r'_lin'
			}
			
			tempfile boot_`b'
			save `boot_`b'', replace	
		}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	

		
		local index =1 			
		foreach type in lin {
			sum ate_w_`type'
			local `var'_se_w_ate_`type' = string(r(sd), "%10.3fc")
			
			sum ate_b_`type'
			local `var'_se_b_ate_`type' = string(r(sd), "%10.3fc")
			
			sum avg_ud_boot_`type'_case
			local `var'_se_mean_`index'_case = string(r(sd), "%10.3fc")
			
			
			local ++index
		}
	}
	
		// make table
		file write fh ///
		"\begin{singlespace}" _n ///
		"\begin{table}[H]" _n ///
  "\centering" _n ///
  "\footnotesize" _n ///
  "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
  "\caption{Heterogeneity in Investigator UD by Child and Investigation Characteristics}" _n ///
  "\begin{threeparttable}" _n ///
"\begin{tabular}{lccccccc}" _n ///
"\hline \hline " _n ///
 "& \textbf{(1)} & \textbf{(2)} & \textbf{(3)} & \textbf{(4)} & \textbf{(5)} & \textbf{(6)} & \textbf{(7)} \\" _n ///
  "& \textbf{All} & \textbf{Male} & \textbf{Female} & \textbf{Young} & \textbf{Old} & \textbf{Physical} & \textbf{No Physical} \\" _n ///
  "& \textbf{Children} & \textbf{Children} & \textbf{Children} & \textbf{Children} & \textbf{Children} & \textbf{Abuse} & \textbf{Abuse} \\" _n ///
"\cline{2-8} " _n ///
 "& \multicolumn{7}{c}{\textit{Panel A: Mean maltreatment risk by race}} \\" _n ///
 "Black children & 0.155 	& `male_b_ate_lin' &  `female_b_ate_lin'  &  `young_b_ate_lin' &  `old_b_ate_lin' &  `abuse_b_ate_lin' &  `neg_b_ate_lin'  \\" _n ///
				"& (0.003)   & (`male_se_b_ate_lin') 	& (`female_se_b_ate_lin') & (`young_se_b_ate_lin') & (`old_se_b_ate_lin') & (`abuse_se_b_ate_lin') & (`neg_se_b_ate_lin')    \\" _n ///
"White children  & 0.175 	& `male_w_ate_lin' &  `female_w_ate_lin'  &  `young_w_ate_lin' &  `old_w_ate_lin' &  `abuse_w_ate_lin' &  `neg_w_ate_lin'  \\" _n ///
				"& (0.003)   & (`male_se_w_ate_lin') & (`female_se_w_ate_lin') & (`young_se_w_ate_lin') & (`old_se_w_ate_lin') & (`abuse_se_w_ate_lin') & (`neg_se_w_ate_lin')    \\ \\" _n ///
 "& \multicolumn{7}{c}{\textit{Panel B: System-wide UD}} \\ \\" _n ///
"Mean across investigators 	& 0.017		 & `male_lin_mn_text_case' & `female_lin_mn_text_case' & `young_lin_mn_text_case' & `old_lin_mn_text_case'  & `abuse_lin_mn_text_case' & `neg_lin_mn_text_case'   \\" _n ///
							"& (0.002)  	& (`male_se_mean_1_case') & (`female_se_mean_1_case') & (`young_se_mean_1_case') & (`old_se_mean_1_case') & (`abuse_se_mean_1_case') & (`neg_se_mean_1_case')   \\ \\" _n ///
 "Number of investigators 	&  814  	&  `n_male' & `n_female'  & `n_young' & `n_old' & `n_abuse' & `n_neg'  \\" _n ///
 
 file write fh ///
        "\hline \hline" _n /// 
		"\end{tabular}" _n ///
		"\begin{tablenotes}" _n ///
		"\item \footnotesize \textit{Notes.} This table summarizes estimates of race-specific mean risk among screened-in calls and average investigator UD, separately by child and investigation characteristics. For each subgroup, we require that an investigator handled at least 100 cases in order to be included in the sample. Therefore, the number of investigators varies across the columns depending on how many investigators in the sample met the requirement. We define young as a child who is 7 years old or younger (the median age in our sample). Estimates come from a linear extrapolation of the variation in Panel B of Figure \ref{fig:extrapolation}. However, the extrapolations are done within the given characteristic. Panel A reports estimates of race-specific average maltreatment risk, while Panel B reports estimates of average (case-weighted) investigator UD. Robust standard errors, two-way clustered at the child and investigator levels, are obtained by a bootstrapping procedure (with 500 replications) and appear in parentheses. \\" _n ///
"\end{tablenotes}" _n ///
"\end{threeparttable}" _n ///
  "\label{tab:main_inv_het}%" _n ///
"\end{table}%" _n ///
"\end{singlespace}" _n _n _n _n _n 

file close fh
macro drop fh


**************************
**TABLE A13
**************************
capture file close fh
capture macro drop fh
capture erase "${output}tableA13.tex"
file open fh using "${output}tableA13.tex", write replace

*(1) Show results without strata adjustment
	use "${cleandata}inv_unadjusted_rates_se_qje.dta", clear
	count 
	local n_inv_rob1 = r(N)
	
	rename (w_nofc b_nofc w_inv6m b_inv6m) (D_w D_b Y_w Y_b)
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	sum bshare 
	local bshare = `r(mean)'
	
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}

	foreach type in lin {
			sum w_ate_`type'
			local w_ate_rob1 = string(r(mean), "%10.3fc")
			
			sum b_ate_`type'
			local b_ate_rob1 = string(r(mean), "%10.3fc")
			
		}
		
		*Generate delta j's:
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen beta_gap0_lin_w=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen beta_gap0_lin_b=(D_b*(1-Y_b))/(1-`b_ate_lin')
		
		gen ate_lin =  bshare*`b_ate_lin' + (1-bshare)*`w_ate_lin'
		
		gen beta_gap_lin = (beta_gap0_lin_w - beta_gap0_lin_b)* (1-ate_lin) + (beta_gap1_lin_w - beta_gap1_lin_b)* (ate_lin)	
	
	// case-weighted mean
	foreach type in lin {	
		sum beta_gap_`type' [aw=count_inv]
		local ud_rob1 = string(`r(mean)', "%10.3fc")
	}
	
	*Bootstrap standard errors:
	use "${cleandata}inv_unadjusted_rates_se_qje.dta", clear
	rename (w_nofc b_nofc w_inv6m b_inv6m) (D_w D_b Y_w Y_b)
	
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	sum bshare 
	local bshare = `r(mean)'
	
		tempfile temp
		save `temp', replace		
		forvalues b = 1/500 {

			use `temp', replace
			
			*generate bootstrap first estimates
			replace D_b = D_b + rnormal(0,b_twse_nofc)
			replace D_w = D_w + rnormal(0,w_twse_nofc)
			replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
			replace Y_w = Y_w + rnormal(0,w_twse_inv6m)
	
			foreach r in b w {
			
				*inverse variance of the investigator FE
				gen `r'_var=1/(`r'_se_inv6m^2)
				
				*grid to plot fitted values
				sum D_`r'
				range `r'_points `r(min)' 1 100

				*linear regression
				reg Y_`r' D_`r' [aw=`r'_var]
				local ate_`r'_lin=_b[_cons]+_b[D_`r']		
			}				
			local ate_lin=`ate_b_lin'*`bshare'+`ate_w_lin'*(1-`bshare')

		*Gen pieces needed for UD 
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen beta_gap0_lin_w=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen beta_gap0_lin_b=(D_b*(1-Y_b))/(1-`b_ate_lin')
		gen ate_lin =  bshare*`b_ate_lin' + (1-bshare)*`w_ate_lin'
	
		gen ud_boot_lin = (beta_gap0_lin_w * (1 - ate_lin) + beta_gap1_lin_w *(ate_lin))  - ///
		(beta_gap0_lin_b * (1 - ate_lin) + beta_gap1_lin_b * (ate_lin))
				
			local index = 1
			// delta 
			foreach type in lin {
				sum ud_boot_`type' [aw=count_inv]
				gen avg_ud_boot_`type'_rob1 = `r(mean)' if _n==1
			
			}
			
			keep avg_*
			keep if _n==1
			foreach r in "w" "b" {
				gen ate_`r'_lin=`ate_`r'_lin'
			}
			
			tempfile boot_`b'
			save `boot_`b'', replace	
		}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	

			foreach type in lin {
			sum ate_w_`type'
			local se_w_ate_rob1 = string(r(sd), "%10.3fc")
			
			sum ate_b_`type'
			local se_b_ate_rob1 = string(r(sd), "%10.3fc")
			
			sum avg_ud_boot_`type'_rob1
			local se_ud_rob1 = string(r(sd), "%10.3fc")
		}
	
	
	
*(2) Show estimates without weighting by the inverse variance of the investigator's subsequent maltreatment rate 
	use "${cleandata}inv_adjusted_rates_se_omitted_qje.dta", clear
	count 
	local n_inv_rob2 = r(N)
	
	rename (w_nofc b_nofc w_inv6m b_inv6m) (D_w D_b Y_w Y_b)
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	sum bshare 
	local bshare = `r(mean)'
	
	foreach r in b w {
			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r'
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}

	foreach type in lin {
			sum w_ate_`type'
			local w_ate_rob2 = string(r(mean), "%10.3fc")
			
			sum b_ate_`type'
			local b_ate_rob2 = string(r(mean), "%10.3fc")			
		}
			
		*Generate delta j's:
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen beta_gap0_lin_w=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen beta_gap0_lin_b=(D_b*(1-Y_b))/(1-`b_ate_lin')
		
		gen ate_lin =  bshare*`b_ate_lin' + (1-bshare)*`w_ate_lin'
		
		gen beta_gap_lin = (beta_gap0_lin_w * (1 - ate_lin) + beta_gap1_lin_w *(ate_lin))  - ///
		(beta_gap0_lin_b * (1 - ate_lin) + beta_gap1_lin_b * (ate_lin))
		
	// case-weighted mean
	foreach type in lin {
	
		sum beta_gap_`type' [aw=count_inv]
		local ud_rob2 = string(`r(mean)', "%10.3fc")
	}
	
	*Bootstrap standard errors:
	use "${cleandata}inv_adjusted_rates_se_omitted_qje.dta", clear
	rename (w_nofc b_nofc w_inv6m b_inv6m) (D_w D_b Y_w Y_b)
	
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	sum bshare 
	local bshare = `r(mean)'
	
		tempfile temp
		save `temp', replace		
		forvalues b = 1/500 {

			use `temp', replace
			
			*generate bootstrap first estimates
			replace D_b = D_b + rnormal(0,b_twse_nofc)
			replace D_w = D_w + rnormal(0,w_twse_nofc)
			replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
			replace Y_w = Y_w + rnormal(0,w_twse_inv6m)
	
			foreach r in b w {

				*grid to plot fitted values
				sum D_`r'
				range `r'_points `r(min)' 1 100

				*linear regression
				reg Y_`r' D_`r'
				local ate_`r'_lin=_b[_cons]+_b[D_`r']		
			}				
			local ate_lin=`ate_b_lin'*`bshare'+`ate_w_lin'*(1-`bshare')

		*Gen pieces needed for UD 
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen beta_gap0_lin_w=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen beta_gap0_lin_b=(D_b*(1-Y_b))/(1-`b_ate_lin')
		gen ate_lin =  bshare*`b_ate_lin' + (1-bshare)*`w_ate_lin'
	
		gen ud_boot_lin = (beta_gap0_lin_w * (1 - ate_lin) + beta_gap1_lin_w *(ate_lin))  - ///
		(beta_gap0_lin_b * (1 - ate_lin) + beta_gap1_lin_b * (ate_lin))
				
			local index = 1
			// delta 
			foreach type in lin {
				sum ud_boot_`type' [aw=count_inv]
				gen avg_ud_boot_`type'_rob2 = `r(mean)' if _n==1
			
			}
			
			keep avg_*
			keep if _n==1
			foreach r in "w" "b" {
				gen ate_`r'_lin=`ate_`r'_lin'
			}
			
			tempfile boot_`b'
			save `boot_`b'', replace	
		}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	

		
		foreach type in lin {
			sum ate_w_`type'
			local se_w_ate_rob2 = string(r(sd), "%10.3fc")
			
			sum ate_b_`type'
			local se_b_ate_rob2 = string(r(sd), "%10.3fc")
			
			sum avg_ud_boot_`type'_rob2
			local se_ud_rob2 = string(r(sd), "%10.3fc")
		}
	
	
*(3) Show robustness to EB correction (the EB subroutine below is directly from the replication code in Arnold, Dobbie, Hull (AER, 2022))
	use "${cleandata}inv_adjusted_rates_se_omitted_qje.dta", clear
	count 
	local n_inv_rob3 = r(N)		
	rename (w_nofc b_nofc w_inv6m b_inv6m) (D_w D_b Y_w Y_b)
	
* empirical Bayes subroutine
capture program drop eb
program def eb, rclass
syntax , beta(string) se(string)
	tempvar weight
	tempvar lambda
	tempvar se2
	gen `se2'=`se'^2
	gen `weight'=1
	forval iter=1/5 {
		summ `beta' [aw=`weight']
		local mn=r(mean)
		local var=r(sd)^2
		summ `se2' [aw=`weight']
		local adj_var=`var'-r(mean)
		disp `adj_var'
		replace `weight'=1/(`adj_var'+`se'^2)
		replace `weight'=0 if `weight'<0
	}
	
	gen lambda_`beta'=`adj_var'*`weight'
	gen post_`beta'=lambda_`beta'*`beta'+(1-lambda_`beta')*`mn'
	gen postvar_`beta'=`adj_var'*(1-lambda_`beta')
	gen post_`beta'_pos=normal(post_`beta'/(sqrt(postvar_`beta')))
	return scalar mean = `mn'
	return scalar var = `adj_var'
end

eb, beta(D_w) se(w_se_nofc)
eb, beta(D_b) se(b_se_nofc)

	replace D_w = post_D_w
	replace D_b = post_D_b 
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b

	sum bshare 
	local bshare = `r(mean)'
	
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}

	foreach type in lin {
			sum w_ate_`type'
			local w_ate_rob3 = string(r(mean), "%10.3fc")
			
			sum b_ate_`type'
			local b_ate_rob3 = string(r(mean), "%10.3fc")			
		}
		
		*Generate delta j's:
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen beta_gap0_lin_w=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen beta_gap0_lin_b=(D_b*(1-Y_b))/(1-`b_ate_lin')
		
		gen ate_lin =  bshare*`b_ate_lin' + (1-bshare)*`w_ate_lin'
		
		gen beta_gap_lin = (beta_gap0_lin_w - beta_gap0_lin_b)* (1-ate_lin) + (beta_gap1_lin_w - beta_gap1_lin_b)* (ate_lin)	
	
	// case-weighted mean
	foreach type in lin {
		sum beta_gap_`type' [aw=count_inv]
		local ud_rob3 = string(`r(mean)', "%10.3fc")
	}
	
	*Bootstrap standard errors:
	use "${cleandata}inv_adjusted_rates_se_omitted_qje.dta", clear
	rename (w_nofc b_nofc w_inv6m b_inv6m) (D_w D_b Y_w Y_b)
	
	eb, beta(D_w) se(w_se_nofc)
	eb, beta(D_b) se(b_se_nofc)

	replace D_w = post_D_w
	replace D_b = post_D_b 
	
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	
	sum bshare 
	local bshare = `r(mean)'
	
		tempfile temp
		save `temp', replace		
		forvalues b = 1/500 {

			use `temp', replace
			
			*generate bootstrap first estimates
			replace D_b = D_b + rnormal(0,b_twse_nofc)
			replace D_w = D_w + rnormal(0,w_twse_nofc)
			replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
			replace Y_w = Y_w + rnormal(0,w_twse_inv6m)
	
			foreach r in b w {
			
				*inverse variance of the investigator FE
				gen `r'_var=1/(`r'_se_inv6m^2)
				
				*grid to plot fitted values
				sum D_`r'
				range `r'_points `r(min)' 1 100

				*linear regression
				reg Y_`r' D_`r' [aw=`r'_var]
				local ate_`r'_lin=_b[_cons]+_b[D_`r']		
			}				
			local ate_lin=`ate_b_lin'*`bshare'+`ate_w_lin'*(1-`bshare')

		*Gen pieces needed for UD 
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen beta_gap0_lin_w=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen beta_gap0_lin_b=(D_b*(1-Y_b))/(1-`b_ate_lin')
		gen ate_lin =  bshare*`b_ate_lin' + (1-bshare)*`w_ate_lin'
	
		gen ud_boot_lin = (beta_gap0_lin_w * (1 - ate_lin) + beta_gap1_lin_w *(ate_lin))  - ///
		(beta_gap0_lin_b * (1 - ate_lin) + beta_gap1_lin_b * (ate_lin))
				
			local index = 1
			
			// delta 
			foreach type in lin {
				sum ud_boot_`type' [aw=count_inv]
				gen avg_ud_boot_`type'_rob3 = `r(mean)' if _n==1
			
			}
			
			keep avg_*
			keep if _n==1
			foreach r in "w" "b" {
				gen ate_`r'_lin=`ate_`r'_lin'
			}
			
			tempfile boot_`b'
			save `boot_`b'', replace	
		}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	

			foreach type in lin {
			sum ate_w_`type'
			local se_w_ate_rob3 = string(r(sd), "%10.3fc")
			
			sum ate_b_`type'
			local se_b_ate_rob3 = string(r(sd), "%10.3fc")
			
			sum avg_ud_boot_`type'_rob3
			local se_ud_rob3 = string(r(sd), "%10.3fc")
		}
		
		
// make table
		file write fh ///
"\begin{table}[htbp]" _n ///
"\begin{singlespace}" _n ///
  "\centering" _n ///
  "\footnotesize" _n ///
  "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
  "\caption{Robustness of Estimates of Maltreatment Risk and Investigator UD}" _n ///
    "\begin{threeparttable}" _n ///
   " \begin{tabular}{lcccc}" _n ///
       "\hline \hline " _n ///
    "      & \textbf{(1)} & \textbf{(2)} & \textbf{(3)} & \textbf{(4)} \\" _n ///
     "     & \textbf{Baseline} & \textbf{No} & \textbf{No} & \textbf{Empirical} \\" _n ///
      "    &       & \textbf{Strata} & \textbf{Weighting} & \textbf{Bayes} \\" _n ///
       "   &       &   \textbf{Adjustment}    &       & \textbf{Shrinkage} \\" _n ///
	  	      " \cmidrule{2-5}" _n ///
        "  & \multicolumn{4}{c}{\textit{Panel A: Subsequent maltreatment risk}} \\" _n ///
    "Black children & 0.155        &   `b_ate_rob1'    		&  `b_ate_rob2'      &  `b_ate_rob3'  \\" _n ///
     "     			& (0.003)      &  (`se_b_ate_rob1')     &  (`se_b_ate_rob2')     & (`se_b_ate_rob3')  \\" _n ///
    "White children & 0.175        &   `w_ate_rob1'    		&  `w_ate_rob2'     		& `w_ate_rob3'  \\" _n ///
    "      			& (0.003)      &  (`se_w_ate_rob1')     &  (`se_w_ate_rob2')     & (`se_w_ate_rob3')  \\" _n ///
     "     			&       &       &       &  \\" _n ///
      "    & \multicolumn{4}{c}{\textit{Panel B: Investigator UD}} \\" _n ///
    "Mean across investigators  & 0.017      &   `ud_rob1'   	    &  `ud_rob2'      & `ud_rob3' \\" _n ///
     "     						& (0.002)     &  (`se_ud_rob1')     &  (`se_ud_rob2')  & (`se_ud_rob3') \\" _n ///
      "    						&       &       			  &       &  \\" _n ///
    "Number of investigators &  814     &  `n_inv_rob1'       &  `n_inv_rob2'      & `n_inv_rob3'  \\" _n 
	
	 file write fh ///
"\hline \hline" _n /// 
 "   \end{tabular}%" _n ///
  "\begin{tablenotes}" _n ///
   " \item \footnotesize \textit{Notes.} This table summarizes estimates of mean maltreatment risk among screened-in calls and average (case-weighted) investigator UD across different specifications. Estimates in Column 2 come from a linear extrapolation of the variation in Panel B of Figure \ref{fig:extrapolation}, but where the placement and subsequent maltreatment rates are estimated without adjusting for ZIP code by year fixed effects. Estimates in Column 3 come from a linear extrapolation of the variation in Panel B of Figure \ref{fig:extrapolation}, but, unlike our baseline extrapolation, the specification is not inversely weighted by the variance of each investigator's subsequent maltreatment rate. Finally, estimates in Column 4 come from a linear extrapolation of the variation in Panel B of Figure \ref{fig:extrapolation}, but where we shrink estimates of the placement rate using conventional Empirical Bayes shrinkage. Robust standard errors, two-way clustered at the child and investigator levels, are obtained by a bootstrapping procedure (with 500 replications) and appear in parentheses." _n ///
"\end{tablenotes}" _n ///
"    \end{threeparttable}" _n ///
"  \label{tab:main_robustness}%" _n ///
"\end{singlespace}" _n ///
"\end{table}%" _n _n _n _n _n 
file close fh
macro drop fh


**************************
**TABLE A14
**************************
capture file close fh
capture macro drop fh
capture erase "${output}tableA14.tex"
file open fh using "${output}tableA14.tex", write replace

foreach xvar in inv2m inv3m inv4m inv5m  {
	use "${cleandata}inv_rates_se_shorter_horizons_qje.dta", clear
	count 
	local n_`xvar' = r(N)
	rename (w_`xvar' b_`xvar') (Y_w Y_b)
		
	sum bshare 
	local bshare = r(mean)
	
	
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_`xvar'^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100
			
			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}

	foreach type in lin {
			sum w_ate_`type'
			local `xvar'_w_ate_`type' = string(r(mean), "%10.3fc")
			
			sum b_ate_`type'
			local `xvar'_b_ate_`type' = string(r(mean), "%10.3fc")
			
		}
		
		*Generate delta j's:
		gen ate_lin =  bshare*`b_ate_lin' + (1-bshare)*`w_ate_lin'
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen beta_gap0_lin_w=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen beta_gap0_lin_b=(D_b*(1-Y_b))/(1-`b_ate_lin')
		
		gen beta_gap_lin = (beta_gap0_lin_w * (1 - ate_lin) + beta_gap1_lin_w *(ate_lin))  - ///
		(beta_gap0_lin_b * (1 - ate_lin) + beta_gap1_lin_b * (ate_lin))
		

	// case-weighted mean
	foreach type in lin {
	
		sum beta_gap_`type' [aw=count_inv]
		local `xvar'_`type'_mn_text_case = string(`r(mean)', "%10.3fc")
	}
	
	use "${cleandata}inv_rates_se_shorter_horizons_qje.dta", clear
	rename (w_`xvar' b_`xvar') (Y_w Y_b)
	
	sum bshare 
	local bshare = `r(mean)'
	
	// bootstrapped standard errors
		tempfile temp
		save `temp', replace		
		forvalues b = 1/500 {

			use `temp', replace
			
			*generate bootstrap estimates
			replace D_b = D_b + rnormal(0,b_twse_fc)
			replace D_w = D_w + rnormal(0,w_twse_fc)
			replace Y_b = Y_b + rnormal(0,b_twse_`xvar')
			replace Y_w = Y_w + rnormal(0,w_twse_`xvar')
	
			foreach r in b w {
			
				*inverse variance of the investigator FE
				gen `r'_var=1/(`r'_se_`xvar'^2)
				
				*grid to plot fitted values
				sum D_`r'
				range `r'_points `r(min)' 1 100
				
				*linear regression
				reg Y_`r' D_`r' [aw=`r'_var]
				gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
				sum `r'_fitted_1 if `r'_points==1
				gen ate_`r'_lin = `r(mean)'
				local ate_`r'_lin=`r(mean)'
			}
				
			local ate_lin=`ate_b_lin'*`bshare'+`ate_w_lin'*(1-`bshare')
						
			// generate UDs
			gen ud_boot_lin=((D_w*Y_w)/`ate_w_lin' ///
				-(D_b*Y_b)/`ate_b_lin')*`ate_lin' ///
			   +((D_w*(1-Y_w))/(1-`ate_w_lin') ///
				-(D_b*(1-Y_b))/(1-`ate_b_lin'))*(1-`ate_lin') 
			
			local index = 1
			// case weighted means 
			foreach type in lin {
				sum ud_boot_`type' [aw=count_inv]
				gen avg_ud_boot_`type'_case = `r(mean)' if _n==1
			
			}
			
			keep avg_*
			keep if _n==1
			foreach r in "w" "b" {
				gen ate_`r'_lin=`ate_`r'_lin'
			}
			
			tempfile boot_`b'
			save `boot_`b'', replace	
		}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	

		
		local index =1 			
		foreach type in lin {
			sum ate_w_`type'
			local `xvar'_se_w_ate_`type' = string(r(sd), "%10.3fc")
			
			sum ate_b_`type'
			local `xvar'_se_b_ate_`type' = string(r(sd), "%10.3fc")
			
			sum avg_ud_boot_`type'_case
			local `xvar'_se_mean_`index'_case = string(r(sd), "%10.3fc")
			
			
			local ++index
		}
	}
	
		// make table
		file write fh ///
"\begin{table}[htbp]" _n ///
  "\centering" _n ///
  "\footnotesize" _n ///
  "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
  "\caption{Estimates of Maltreatment Risk and Investigator UD (Shorter Time Horizons)}" _n ///
  "\begin{threeparttable}" _n ///
    "\begin{tabular}{lccccccc}" _n ///
      "\hline \hline " _n ///
          "& \textbf{(1)} & \textbf{(2)} & \textbf{(3)} & \textbf{(4)} & \textbf{(5)}  \\" _n ///
          "& \textbf{Baseline} & \textbf{Inv.} & \textbf{Inv.} & \textbf{Inv.} & \textbf{Inv.}   \\" _n ///
          "&       & \textbf{within 5} & \textbf{within 4} & \textbf{within 3} & \textbf{within 2}   \\" _n ///
          "&       & \textbf{months} & \textbf{months} & \textbf{months} & \textbf{months}   \\" _n ///
              "      \cmidrule{2-6}" _n ///
          "& \multicolumn{5}{c}{\textit{Panel A: Subsequent maltreatment risk}} \\" _n ///
    "Black children & 0.155 & `inv5m_b_ate_lin' & `inv4m_b_ate_lin'  & `inv3m_b_ate_lin'  & `inv2m_b_ate_lin'   \\" _n ///
	"				& (0.003) & (`inv5m_se_b_ate_lin') & (`inv4m_se_b_ate_lin') & (`inv3m_se_b_ate_lin') & (`inv2m_se_b_ate_lin')  \\" _n ///
    "White children 	& 0.175 & `inv5m_w_ate_lin' & `inv4m_w_ate_lin'  & `inv3m_w_ate_lin'  & `inv2m_w_ate_lin'   \\" _n ///
	"				& (0.003) & (`inv5m_se_w_ate_lin') & (`inv4m_se_w_ate_lin') & (`inv3m_se_w_ate_lin') & (`inv2m_se_w_ate_lin')   \\" _n ///
    "      &       &       &       &       &               \\" _n ///
     "     & \multicolumn{5}{c}{\textit{Panel B: Investigator UD}} \\" _n ///
    "Mean across investigators & 0.017 &  `inv5m_lin_mn_text_case' & `inv4m_lin_mn_text_case' & `inv3m_lin_mn_text_case'  & `inv2m_lin_mn_text_case'   \\" _n ///
							  "& (0.002) & (`inv5m_se_mean_1_case') & (`inv4m_se_mean_1_case') & (`inv3m_se_mean_1_case') & (`inv2m_se_mean_1_case')   \\" _n ///
         " &       &       &       &       &           \\" _n ///
    "Number of investigators & 814   & `n_inv5m'   & `n_inv4m'  & `n_inv3m'  & `n_inv2m'  \\" _n 
	
	 file write fh ///
"\hline \hline" _n /// 
 "   \end{tabular}%" _n ///
  "\begin{tablenotes}" _n ///
   " \item \footnotesize \textit{Notes.} This table summarizes estimates of mean maltreatment risk among screened-in calls and average (case-weighted) investigator UD for different re-investigation time horizons. Panel A reports estimates of race-specific average re-investigation risk, while Panel B reports estimates of average investigator UD. Estimates in Panel A come from a linear extrapolation of the variation in Panel B of Figure \ref{fig:extrapolation}. Robust standard errors, two-way clustered at the child and investigator levels, are obtained by a bootstrapping procedure (500 times) and appear in parentheses. \\" _n ///
"\end{tablenotes}" _n ///
"    \end{threeparttable}" _n ///
"  \label{tab:shorter_times}%" _n ///
"\end{table}%" _n _n _n _n _n 

file close fh
macro drop fh


**************************
**TABLE A15
**************************
capture file close fh
capture macro drop fh
capture erase "${output}tableA15.tex"
file open fh using "${output}tableA15.tex", write replace
	
	foreach xvar in inv6m inv6m_sub inv6m_fc inv6m_p inv6m_n inv6m_p_sub inv6m_n_sub {
	use "${cleandata}inv_adjusted_rates_se_omitted_qje.dta", clear
	
	sum bshare 
	local bshare = r(mean)

	count 
	local n_`xvar' = r(N)
	
	rename (w_nofc b_nofc w_`xvar' b_`xvar') (D_w D_b Y_w Y_b)
	rename (w_se_nofc b_se_nofc) (w_se_fe_nofc b_se_fe_nofc)
	
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
		
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_`xvar'^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100
			
			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}

	foreach type in lin {
			sum w_ate_`type'
			local `xvar'_w_ate_`type' = string(r(mean), "%10.3fc")
			
			sum b_ate_`type'
			local `xvar'_b_ate_`type' = string(r(mean), "%10.3fc")
			
		}
		
		*Generate delta j's:
		gen ate_lin =  bshare*`b_ate_lin' + (1-bshare)*`w_ate_lin'
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen beta_gap0_lin_w=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen beta_gap0_lin_b=(D_b*(1-Y_b))/(1-`b_ate_lin')
		
		gen beta_gap_lin = (beta_gap0_lin_w * (1 - ate_lin) + beta_gap1_lin_w *(ate_lin))  - ///
		(beta_gap0_lin_b * (1 - ate_lin) + beta_gap1_lin_b * (ate_lin))
		

	// case-weighted mean
	foreach type in lin {
	
		sum beta_gap_`type' [aw=count_inv]
		local `xvar'_`type'_mn_text_case = string(`r(mean)', "%10.3fc")
	}
	
	*Now, compute bootstrapped standard errors
	use "${cleandata}inv_adjusted_rates_se_omitted_qje.dta", clear
	sum bshare 
	local bshare = r(mean)
	
	rename (w_nofc b_nofc w_`xvar' b_`xvar') (D_w D_b Y_w Y_b)
	rename (w_twse_nofc b_twse_nofc) (w_se_fe_nofc b_se_fe_nofc)
	
	gen D_w2 = D_w*D_w 
	gen D_b2 = D_b*D_b
	
	// bootstrapped standard errors
		tempfile temp
		save `temp', replace		
		forvalues b = 1/500 {

			use `temp', replace
			
			*generate bootstrap first estimates
			replace D_b = D_b + rnormal(0,b_se_fe_nofc)
			replace D_w = D_w + rnormal(0,w_se_fe_nofc)
			replace Y_b = Y_b + rnormal(0,b_twse_`xvar')
			replace Y_w = Y_w + rnormal(0,w_twse_`xvar')
	
			foreach r in b w {
			
				*inverse variance of the investigator FE
				gen `r'_var=1/(`r'_se_`xvar'^2)
				
				*grid to plot fitted values
				sum D_`r'
				range `r'_points `r(min)' 1 100
				
				*linear regression
				reg Y_`r' D_`r' [aw=`r'_var]
				gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
				sum `r'_fitted_1 if `r'_points==1
				gen `r'_ate_lin = `r(mean)'
				local ate_`r'_lin=`r(mean)'				
				}
				
			local ate_lin=`ate_b_lin'*`bshare'+`ate_w_lin'*(1-`bshare')
						
			// generate UDs
			gen boot_w = ((D_w*Y_w)/`ate_w_lin')
			gen boot_b = ((D_b*Y_b)/`ate_b_lin')
			gen boot0_w = (D_w*(1-Y_w))/(1-`ate_w_lin')
			gen boot0_b = (D_b*(1-Y_b))/(1-`ate_b_lin')			
			
			gen ud_boot_lin=(boot_w - boot_b)*`ate_lin' ///
			   +(boot0_w - boot0_b)*(1-`ate_lin') 
			
			local index = 1
			// case weighted means 
			foreach type in lin {
				sum ud_boot_`type' [aw=count_inv]
				gen avg_ud_boot_`type'_case = `r(mean)' if _n==1
			
			}
			
			keep avg_*
			keep if _n==1
			foreach r in "w" "b" {
				gen ate_`r'_lin=`ate_`r'_lin'
			}
			
			tempfile boot_`b'
			save `boot_`b'', replace	
		}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	
		
		local index =1 			
		foreach type in lin {
			sum ate_w_`type'
			local `xvar'_se_w_ate_`type' = string(r(sd), "%10.3fc")
			
			sum ate_b_`type'
			local `xvar'_se_b_ate_`type' = string(r(sd), "%10.3fc")
			
			sum avg_ud_boot_`type'_case
			local `xvar'_se_mean_`index'_case = string(r(sd), "%10.3fc")
			
			
			local ++index
		}
	}
	
		// make table
		file write fh ///
"\begin{table}[htbp]" _n ///
  "\centering" _n ///
  "\footnotesize" _n ///
  "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
  "\caption{Estimates of Maltreatment Risk and Investigator UD (Alternative Outcomes)}" _n ///
  "\begin{threeparttable}" _n ///
    "\begin{tabular}{lcccccccc}" _n ///
    "\hline \hline " _n ///
          "& \textbf{(1)} 		& \textbf{(2)} 			& \textbf{(3)} 			& \textbf{(4)} 			& \textbf{(5)}  		& \textbf{(6)}   			& \textbf{(7)}  							 \\" _n ///
          "& \textbf{Baseline} & \textbf{Confirmed} 	& \textbf{Placed} 		& \textbf{Abuse inv.} 	& \textbf{Neglect Inv.}  & \textbf{Confirmed}  & \textbf{Confirmed}					\\" _n ///
          "&       				& \textbf{victim within} & \textbf{within 6}	 & \textbf{within 6} 	& \textbf{within 6}   & \textbf{abuse}  		& \textbf{neglect}  								\\" _n ///
          "&      				& \textbf{6 months} 	& \textbf{months} 		& \textbf{months} 		& \textbf{months}   	& \textbf{within}   	& \textbf{within}   									\\" _n ///
          "&      				& 						& 						& 						&   					&  \textbf{6 months}  	& 	\textbf{6 months} 						\\" _n ///
              "      \cmidrule{2-8}" _n ///
          "& \multicolumn{7}{c}{\textit{Panel A: Subsequent maltreatment risk}} \\" _n ///
    "Black children & `inv6m_b_ate_lin' & `inv6m_sub_b_ate_lin' & `inv6m_fc_b_ate_lin'  & `inv6m_p_b_ate_lin'  & `inv6m_n_b_ate_lin' & `inv6m_p_sub_b_ate_lin' & `inv6m_n_sub_b_ate_lin'  \\" _n ///
	"				& (`inv6m_se_b_ate_lin') & (`inv6m_sub_se_b_ate_lin') & (`inv6m_fc_se_b_ate_lin') & (`inv6m_p_se_b_ate_lin') & (`inv6m_n_se_b_ate_lin') & (`inv6m_p_sub_se_b_ate_lin') & (`inv6m_n_sub_se_b_ate_lin')  \\" _n ///
    "White children 	& `inv6m_w_ate_lin' & `inv6m_sub_w_ate_lin' & `inv6m_fc_w_ate_lin'  & `inv6m_p_w_ate_lin'  & `inv6m_n_w_ate_lin'  & `inv6m_p_sub_w_ate_lin'  & `inv6m_n_sub_w_ate_lin'    \\" _n ///
	"				& (`inv6m_se_w_ate_lin') & (`inv6m_sub_se_w_ate_lin') & (`inv6m_fc_se_w_ate_lin') & (`inv6m_p_se_w_ate_lin') & (`inv6m_n_se_w_ate_lin') & (`inv6m_p_sub_se_w_ate_lin') & (`inv6m_n_sub_se_w_ate_lin')   \\" _n ///
    "      &       &       &       &       &       &     & &    \\" _n ///
     "     & \multicolumn{7}{c}{\textit{Panel B: Investigator UD}} \\" _n ///
    "Mean across investigators & `inv6m_lin_mn_text_case' &  `inv6m_sub_lin_mn_text_case' & `inv6m_fc_lin_mn_text_case' & `inv6m_p_lin_mn_text_case'  & `inv6m_n_lin_mn_text_case' & `inv6m_p_sub_lin_mn_text_case'  & `inv6m_n_sub_lin_mn_text_case'  \\" _n ///
							  "& (`inv6m_se_mean_1_case') & (`inv6m_sub_se_mean_1_case') & (`inv6m_fc_se_mean_1_case') & (`inv6m_p_se_mean_1_case') & (`inv6m_n_se_mean_1_case') & (`inv6m_p_sub_se_mean_1_case') & (`inv6m_n_sub_se_mean_1_case')   \\" _n ///
         " &       &       &       &       &       &  & &   \\" _n ///
    "Number of investigators & `n_inv6m'   & `n_inv6m_sub'   & `n_inv6m_fc'  & `n_inv6m_p'  & `n_inv6m_n' & `n_inv6m_p_sub'  & `n_inv6m_n_sub'   \\" _n 
	
	 file write fh ///
"\hline \hline" _n /// 
 "   \end{tabular}%" _n ///
  "\begin{tablenotes}" _n ///
   " \item \footnotesize \textit{Notes.} This table summarizes estimates of mean maltreatment risk among screened-in calls and average (case-weighted) investigator UD for different outcome variables. Panel A reports estimates of race-specific average re-investigation risk, while Panel B reports estimates of average investigator UD. Estimates in Panel A come from a linear extrapolation of the variation in Panel B of Figure \ref{fig:extrapolation}. Robust standard errors, two-way clustered at the child and investigator levels, are obtained by a bootstrapping procedure (with 500 replications) and appear in parentheses." _n ///
"\end{tablenotes}" _n ///
"    \end{threeparttable}" _n ///
"  \label{tab:omitted_payoffs_weighted}%" _n ///
"\end{table}%" _n _n _n _n _n 

file close fh
macro drop fh


**************************
**TABLE A16
**************************
capture file close fh
capture macro drop fh
capture erase "${output}tableA16.tex"
file open fh using "${output}tableA16.tex", write replace

*Estimate subsequent maltreatment risk and investigator UD in 2017-2019 sample, among mandated reporters 
use "${cleandata}inv_later_sample_m_qje.dta", clear
count 
local n_inv_rob1 = r(N)
	
	sum bshare 
	local bshare = `r(mean)'
	
	foreach r in b w {
		
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}

	foreach type in lin {
			sum w_ate_`type'
			local w_ate_rob1 = string(r(mean), "%10.3fc")
			
			sum b_ate_`type'
			local b_ate_rob1 = string(r(mean), "%10.3fc")
			
		}
		
		*Generate delta j's:
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen beta_gap0_lin_w=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen beta_gap0_lin_b=(D_b*(1-Y_b))/(1-`b_ate_lin')
		
		gen ate_lin =  bshare*`b_ate_lin' + (1-bshare)*`w_ate_lin'
		
		gen beta_gap_lin = (beta_gap0_lin_w - beta_gap0_lin_b)* (1-ate_lin) + (beta_gap1_lin_w - beta_gap1_lin_b)* (ate_lin)	
	
	// case-weighted mean
	foreach type in lin {
	
		sum beta_gap_`type' [aw=count_inv]
		local ud_rob1 = string(`r(mean)', "%10.3fc")
	}

	
	*Bootstrap standard errors:
	use "${cleandata}inv_later_sample_m_qje.dta", clear
	sum bshare 
	local bshare = `r(mean)'
	
		tempfile temp
		save `temp', replace		
		forvalues b = 1/500 {

			use `temp', replace
			
			*generate bootstrap first estimates
			replace D_b = D_b + rnormal(0,b_twse_fc)
			replace D_w = D_w + rnormal(0,w_twse_fc)
			replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
			replace Y_w = Y_w + rnormal(0,w_twse_inv6m)
	
			foreach r in b w {
			
				*inverse variance of the investigator FE
				gen `r'_var=1/(`r'_se_inv6m^2)
				
				*grid to plot fitted values
				sum D_`r'
				range `r'_points `r(min)' 1 100

				*linear regression
				reg Y_`r' D_`r' [aw=`r'_var]
				local ate_`r'_lin=_b[_cons]+_b[D_`r']		
			}				
			local ate_lin=`ate_b_lin'*`bshare'+`ate_w_lin'*(1-`bshare')

		*Gen pieces needed for each UD 
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen beta_gap0_lin_w=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen beta_gap0_lin_b=(D_b*(1-Y_b))/(1-`b_ate_lin')
		gen ate_lin =  bshare*`b_ate_lin' + (1-bshare)*`w_ate_lin'
	
		gen ud_boot_lin = (beta_gap0_lin_w * (1 - ate_lin) + beta_gap1_lin_w *(ate_lin))  - ///
		(beta_gap0_lin_b * (1 - ate_lin) + beta_gap1_lin_b * (ate_lin))
				
			local index = 1
			// delta 
			foreach type in lin {
				sum ud_boot_`type' [aw=count_inv]
				gen avg_ud_boot_`type' = `r(mean)' if _n==1
			
			}
			
			keep avg_*
			keep if _n==1
			foreach r in "w" "b" {
				gen ate_`r'_lin=`ate_`r'_lin'
			}
			
			tempfile boot_`b'
			save `boot_`b'', replace	
		}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	

			foreach type in lin {
			sum ate_w_`type'
			local se_w_ate_rob1 = string(r(sd), "%10.3fc")
			
			sum ate_b_`type'
			local se_b_ate_rob1 = string(r(sd), "%10.3fc")
			
			sum avg_ud_boot_`type'
			local se_ud_rob1 = string(r(sd), "%10.3fc")
		}
		

*Estimate subsequent maltreatment risk and investigator UD in 2017-2019 sample, among non-mandated reporters 		
use "${cleandata}inv_later_sample_n_qje.dta", clear
count 
local n_inv_rob2 = r(N)

	sum bshare 
	local bshare = `r(mean)'
	
	foreach r in b w {
			*inverse variance of the investigator FE
			gen `r'_var=1/(`r'_se_inv6m^2)

			*grid to plot fitted values
			sum D_`r'
			range `r'_points `r(min)' 1 100

			*linear regression
			reg Y_`r' D_`r' [aw=`r'_var]
			gen `r'_fitted_1=_b[_cons]+`r'_points*_b[D_`r']
			sum `r'_fitted_1 if `r'_points==1
			gen `r'_ate_lin = `r(mean)'
			local `r'_ate_lin=`r(mean)'
	}

	foreach type in lin {
			sum w_ate_`type'
			local w_ate_rob2 = string(r(mean), "%10.3fc")
			
			sum b_ate_`type'
			local b_ate_rob2 = string(r(mean), "%10.3fc")
			
		}
		
		*Generate delta j's:

		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen beta_gap0_lin_w=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen beta_gap0_lin_b=(D_b*(1-Y_b))/(1-`b_ate_lin')
		
		gen ate_lin =  bshare*`b_ate_lin' + (1-bshare)*`w_ate_lin'
		
		gen beta_gap_lin = (beta_gap0_lin_w - beta_gap0_lin_b)* (1-ate_lin) + (beta_gap1_lin_w - beta_gap1_lin_b)* (ate_lin)	
	
	// case-weighted mean
	foreach type in lin {
	
		sum beta_gap_`type' [aw=count_inv]
		local ud_rob2 = string(`r(mean)', "%10.3fc")
		display `ud_rob2'
	}

	
	*Bootstrap standard errors:
	use "${cleandata}inv_later_sample_n_qje.dta", clear
	sum bshare 
	local bshare = `r(mean)'
	
		tempfile temp
		save `temp', replace		
		forvalues b = 1/500 {

			use `temp', replace
			
			*generate bootstrap first estimates
			replace D_b = D_b + rnormal(0,b_twse_fc)
			replace D_w = D_w + rnormal(0,w_twse_fc)
			replace Y_b = Y_b + rnormal(0,b_twse_inv6m)
			replace Y_w = Y_w + rnormal(0,w_twse_inv6m)
	
			foreach r in b w {
			
				*inverse variance of the investigator FE
				gen `r'_var=1/(`r'_se_inv6m^2)
				
				*grid to plot fitted values
				sum D_`r'
				range `r'_points `r(min)' 1 100

				*linear regression
				reg Y_`r' D_`r' [aw=`r'_var]
				local ate_`r'_lin=_b[_cons]+_b[D_`r']		
			}				
			local ate_lin=`ate_b_lin'*`bshare'+`ate_w_lin'*(1-`bshare')

		*Gen pieces needed for each UD 
		*Linear:
		gen beta_gap1_lin_w=(D_w*Y_w)/`w_ate_lin'
		gen beta_gap1_lin_b=(D_b*Y_b)/`b_ate_lin' 
		gen beta_gap0_lin_w=(D_w*(1-Y_w))/(1-`w_ate_lin')
		gen beta_gap0_lin_b=(D_b*(1-Y_b))/(1-`b_ate_lin')
		gen ate_lin =  bshare*`b_ate_lin' + (1-bshare)*`w_ate_lin'
	
		gen ud_boot_lin = (beta_gap0_lin_w * (1 - ate_lin) + beta_gap1_lin_w *(ate_lin))  - ///
		(beta_gap0_lin_b * (1 - ate_lin) + beta_gap1_lin_b * (ate_lin))
				
			local index = 1
			// delta 
			foreach type in lin {
				sum ud_boot_`type' [aw=count_inv]
				gen avg_ud_boot_`type' = `r(mean)' if _n==1
			
			}
			
			keep avg_*
			keep if _n==1
			foreach r in "w" "b" {
				gen ate_`r'_lin=`ate_`r'_lin'
			}
			
			tempfile boot_`b'
			save `boot_`b'', replace	
		}

		use `boot_1', replace

		forvalues b = 2/500 {

			append using `boot_`b''
			
		}	

			foreach type in lin {
			sum ate_w_`type'
			local se_w_ate_rob2 = string(r(sd), "%10.3fc")
			
			sum ate_b_`type'
			local se_b_ate_rob2 = string(r(sd), "%10.3fc")
			
			sum avg_ud_boot_`type'
			local se_ud_rob2 = string(r(sd), "%10.3fc")
		}
				
		
		
// make table
		file write fh ///
"\begin{table}[htbp]" _n ///
"\begin{singlespace}" _n ///
  "\centering" _n ///
  "\footnotesize" _n ///
  "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
  "\caption{Heterogeneity in Investigator UD by Reporting Source}" _n ///
    "\begin{threeparttable}" _n ///
   " \begin{tabular}{lccc}" _n ///
    "\hline \hline " _n ///
    "      & \textbf{(1)} & \textbf{(2)} & \textbf{(3)} \\" _n ///
     "     & \textbf{Baseline} & \textbf{Call from} & \textbf{Call from}  \\" _n ///
      "    & \textbf{} & \textbf{Mandated} & \textbf{Non-mandated} \\" _n ///
       "   & \textbf{} & \textbf{Reporter} & \textbf{Reporter}  \\" _n ///
	      " \cmidrule{2-4}" _n ///
        "  & \multicolumn{3}{c}{\textit{Panel A: Subsequent maltreatment risk}} \\" _n ///
    "Black children &  0.155     &  `b_ate_rob1'     &   `b_ate_rob2'      \\" _n ///
    "     			 &  (0.003)     &  (`se_b_ate_rob1')     &  (`se_b_ate_rob2')       \\" _n ///
    "White children & 0.175      &  `w_ate_rob1'     &   `w_ate_rob2'     \\" _n ///
    "      			&  (0.003)     & (`se_w_ate_rob1')      & (`se_w_ate_rob2')       \\" _n ///
     "     			&       &       &        \\" _n ///
      "    & \multicolumn{3}{c}{\textit{Panel B: Investigator UD}} \\" _n ///
    "Mean across investigators & 0.017      & `ud_rob1'      & `ud_rob2'       \\" _n ///
     "     						&  (0.002)    &   (`se_ud_rob1')     &  (`se_ud_rob2')      \\" _n ///
      "    						&       &       &        \\" _n ///
    "Number of investigators & 814      &  `n_inv_rob1'     &    `n_inv_rob2'     \\" _n 
	
	 file write fh ///
"\hline \hline" _n /// 
 "   \end{tabular}%" _n ///
  "\begin{tablenotes}" _n ///
   " \item \footnotesize \textit{Notes.} This table summarizes estimates of mean maltreatment risk among screened-in calls and average (case-weighted) investigator UD. Columns 2 and 3 come from screened-in calls in the screener analysis sample, for which we have information on the reporting source of the maltreatment allegation. A mandated reporter is defined as someone who is either educational personnel, medical personnel, a social worker, or a member of law enforcement. Panel A reports estimates of race-specific average re-investigation risk, while Panel B reports estimates of average investigator UD. Estimates in Panel A come from a linear extrapolation of the variation in Panel B of Figure \ref{fig:extrapolation}, but estimated on the subset of screened-in calls in the screener sample. Robust standard errors, two-way clustered at the child and investigator levels, are obtained by a bootstrapping procedure (with 500 replications) and appear in parentheses." _n ///
"\end{tablenotes}" _n ///
"    \end{threeparttable}" _n ///
"  \label{tab:het_reporter}%" _n ///
"\end{singlespace}" _n ///
"\end{table}%" _n _n _n _n _n 

file close fh
macro drop fh